Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the convention on placing curly braces in Objective-C? [closed]

I have seen different conventions for Objective-C (Cocoa/Cocoa Touch) of placing the curly braces.

The two that I have seen are:

- (void)dealloc {
    [super dealloc];
}

vs.

- (void) dealloc
{
    [super dealloc];
}

This confuses me because I would expect that for such a rather small community there should be only one convention.

Which one of the two is more common?

like image 564
Raphael Avatar asked Jul 17 '11 13:07

Raphael


1 Answers

I don't think there's any canonical answer to this (whether speaking in terms of objective-c or any other language). Personally I prefer:

- (void)dealloc {
  [super dealloc];
}

...but there are certainly a lot of people who prefer the alternate style, as well. As for which is more common, example code provided by Apple appears to prefer the first style (brace on the same line), so that would be a safe bet as the more common pattern. I do recall stumbling across an older Apple coding conventions document which recommended the second style (brace on the next line), however (but it also recommended using two spaces instead of 4 for indents, which makes that document garbage in my opinion). You might as well just pick your preference.

The only thing that I would recommend is that you should never mix both styles in a single source file. Pick one and stick with it. And if you're editing a third-party source file that uses one convention, follow that same convention instead of using the alternate format. Then at least your coding style will always be consistent per compilation-unit.

like image 177
aroth Avatar answered Oct 25 '22 15:10

aroth