inessential by Brent Simmons

Code Formatting Style

On the Debug podcast (which I adore), Craig Hockenberry (whom I adore) mentioned that my code formatting style is crazy.

He may be right. I’ll let you judge.

Here’s a simple example from a view controller’s init method:

- (id)initWithAccount:(GBAccount *)account {

    self = [self initWithNibName:@"Settings\_iPhone" bundle:nil];
    if (self == nil)
        return nil;

    _account = account;
    return self;
}

My formatting style is pretty much K&R style, plucked straight from the C Programming Book — with one modification: opening braces for methods and functions appear at the end of the line rather than on the next line.

With K&R style, you put the opening brace at the end of the line for other blocks (if, while, etc.) — but not with functions. I prefer the more consistent approach of putting the brace at the end of the line for all blocks.

You might not agree with its readability — but I find it cleaner and easier to scan my style than this:

- (id)initWithAccount:(GBAccount *)account
{
    self = [self initWithNibName:@"Settings\_iPhone" bundle:nil];
    if (self == nil)
        return nil;

    _account = account;
    return self;
}

And much easier than this:

- (id)initWithAccount:(GBAccount *)account
{
    self = [self initWithNibName:@"Settings\_iPhone" bundle:nil];
    if (self == nil)
    {
        return nil;
    }

    _account = account;
    return self;
}

Or this:

- (id)initWithAccount:(GBAccount *)account
{
    self = [self initWithNibName:@"Settings\_iPhone" bundle:nil];
    if (self != nil)
    {
        _account = account;
    }

    return self;
}

It’s a matter of what you’re used to, I think — I don’t for one second contend that there are right and wrong ways. There aren’t. Consistency matters more, because any reasonable style is readable and scannable as long as it’s consistent.