If there's one thing I've spent way too much time thinking about, it's the way I format code (bracing and spacing). I know eventually I could get used to any reasonable style, so the time I spend reformatting Xcode's generated code is useless busy work. All I really want is Apple to tell me “this is the way we want you to do it.” While Apple's example projects don't really have a consistent style, the code that Xcode generates for file templates and autocompletion does — the one true brace style.
So my style has evolved from something like this when I started working for myself last September:
- (void) myMethod: (id) sender
{
for( int i; i < 10; 1++ )
{
printf( @"%d", i );
}
}
To something more like Allman style:
- (void)myMethod:(id)sender
{
for (int i; i < 10; i++)
{
printf(@"%d", i);
}
}
And finally to K&R / 1TBS
- (void)myMethod:(id)sender
{
for (int i; i < 10; i++) {
printf(@"%d", i);
}
}
So braces for method and function definitions go on the next line, everything else goes on on the same line. Are there practical reasons this style is better or worse than others? I'm sure. The style I was using before going independent spaces everything out a lot, so whatever reason to like it or not, there's a lot of reformatting that's going to occur, so moving to the second style was mostly to avoid that.
Always putting braces on the next line didn't always work well either though, particularly with blocks. I just couldn't find a way to make blocks look passably decent or work with Xcode's auto-indenting while putting their opening brace on the next line.
Xcode's autoindenting turns what would be this:
dispatch_async(dispatch_get_main_queue(), ^{
// do something here
});
Into this:
dispatch_async(dispatch_get_main_queue(),
^{
// do something here
});
But the biggest reason for making this switch is that's the closest thing to a supported-style Xcode has — all of the file templates lay method definitions out this way, and so does autocomplete. I'm not working against my tools anymore, I don't need to waste time reformatting, and that becomes time I can spend getting real work done.