How I Format Code

Brent Simmons responded on his blog today to something said on the Debug podcast (recommended) about his coding style being crazy. I wondered if I’d missed something when I heard it, since I’ve seen Brent’s code in examples, and thought it was pretty normal looking at the time.

The example he gave was of creating a new view controller:

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

<pre><code>self = [self initWithNibName:@"Settings_iPhone" bundle:nil];
if (self == nil)
    return nil;

_account = account;
return self;


And noted:

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.

My style is similar to Brent’s, but with a couple of differences. I put the braces for methods and functions on the next line, I always using braces for loops and conditionals, and I only ever have one return. My version of the same method would look like this:

- (id)initWithAccount:(GBAccount *)account { self = [self initWithNibName:@“Settings_iPhone” bundle:nil]; if (self != nil) { _account = account; } return self; }

The biggest reason I chose this style is that it requires the least reformatting of Xcode’s template code. Where I used to work we used a very specific and very non-standard way of formatting, and after a while I got really tired of always having to reformat every piece of template or autocomplete code before I could actually do anything, so I tried to pick the style that would eliminate that as much as possible.

Collin Donnell @collin