Last Updated: February 25, 2016
·
299
· sgrankin

Comma operator abuse

Some code just doesn't deserve to have extra newlines dedicated to it. For example:

- (instancetype)init
{
    [self doesNotRecognizeSelector:_cmd];
    return nil;
}

That return will never get hit, but the misguided compiler rightly warns you that you never actually returned everything.

Well, the comma operator is here for you:

- (instancetype)init
{
    return ([self doesNotRecognizeSelector:_cmd], nil);
}

This lets you do neater deallocs as well:

- (void)dealloc
{
    if (_cfTypeRef) 
        _cfTypeRef = (CFRelease(_cfTypeRef), nil);
}