views:

194

answers:

5

I've been teaching myself Objective-C for about 6 months and like using the language a lot. However I haven't found any good coding standards, so the code I write always ends up looking like an inconsistent mess.

Things like naming conventions carry over just fine, but spacing, indentation and the (impossible?) 80 character line-width aren't working out so well.

What conventions do you use with Objective-C?

Here is a small example of something that isn't working:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {

        self.navigationItem.leftBarButtonItem = 
        [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                       target:self.parentViewController 
                                                       action:@selector(done:)] autorelease];

        NSString* units = [[NSString alloc] initWithFormat:@"%@", @"oz"];
        NSString* optionOne = [[NSString alloc] initWithFormat:@"[%d%@] Calculate", 100, units];

        self.options = [[NSMutableArray alloc] initWithObjects: 
                        optionOne, 
                        @"Configure Portions", 
                        @"Configure Notifications",
                        @"Help",
                        nil];

        [units release];
        [optionOne release];
        [tableView reloadData];
    }
    return self;
}
A: 

Not that I use and/or like it, but Google's Objective-C Style Guide is worth a mention and a read.

cobbal
+2  A: 

Great question, thanks for asking it.

A few of my personal coding standards:

  1. I don't stick to 80 characters, but I try to stay under 120 or so. Obj-C is a wordy language with "named" arguments, and Cocoa is an even wordier framework. I rarely need to edit code on a VT220.
  2. I don't usually split long method calls with the ":" lined up vertically like Xcode wants you to do. I favor the traditional straight ahead, with lines wrapped as necessary, indented by one tab stop.
  3. Where this becomes really unwieldy, I break out creation and use of objects over multiple lines. E.g. in the above, I'd likely create the options array on one line, and do [self setOptions:...] on the next one. This makes debugging simpler anyways.
  4. I don't use dot notation for property access, since I find it hides behavior. I use the traditional [object property] notation.
  5. I have never satisfactorily resolved the naming of ivars vs. locals. Xcode colors them differently, which is usually all I need, but the MSFT guy deep inside me still thinks instance scope prefixing is useful, e.g. m_ or at least _. But I usually don't, because it's ugly to look at. And goodness knows we Apple people hate ugly things. :)

(For what it's worth, in your example above, you can get an autoreleased string directly by using -[NSString stringWithFormat:...] instead of the alloc/init/release.)

quixoto
+1. I follow pretty much the same guidelines, and am also not too fond of [Eastern Polish Christmas Tree notation](http://www.kuro5hin.org/story/2004/6/1/43942/41236).
dreamlax
@dreamlax: +1 for that notation name.
quixoto
Beyond these excellent suggestions, I also use whatever indention I get when I let Xcode re-indent the whole file. Manual indention is a waste of time and, fortunately, Xcode does a mostly decent job.
bbum
Good note about line length. Most of the open-source Objective-C projects I've contributed to over the years use a 120-character line limit, but ignore the 80-character limit -- as you say, Objective-C is a bit too "wordy" to stick to 80 characters. And usually the 120-character limit is a soft limit -- if you go over by a bit but your code is readable, it's okay.
mipadi
I have no problem getting my lines within the 80 character limit. Apart from that, I put in more horizontal white space than Apple. e.g. between the colon and the type of the parameter.
JeremyP
+3  A: 

Apple's coding guidelines can be found here: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html

Jon Hull
A: 

This is another good source: (I am new so it wouldn't let me post two links in the same answer) http://cocoadevcentral.com/articles/000082.php (Cocoa Style for Obj C part 1 of 2)

part 2 is the same link but ending in 000083.php

Jon Hull
A: 

This is probably the dissenting opinion around here but… I don't indent single lines at all, I turn on word-wrap. The advantage of this is that you can shrink/stretch your windows and the code always looks good, plus you don't have to waste any time messing around with newlines and tabs/spaces trying to make your code look acceptable.

kubi