On Apr 14, 2008, at 10:53 PM, Adam Gerson wrote:

In cocoa its very tempting to write a single line of code like:
NSManagedObject *selectedTreeObject = [[[[[self delegate]
mainWindowController] treeController] selectedObjects]
objectAtIndex:0];

or to flush it out in to individual lines:

NSWindowController *mainWindow = [[self delegate] mainWindowController];
NSTreeController *treeController = [mainWindow treeController];
NSArray *selectedTreeObjects = [treeController selectedObjects];
NSManagedObject *selectedTreeObject = [selectedTreeObjects objectAtIndex:0];


I'm not sure that you're going to find 100% agreement on this topic. I can tell you what I do, but I suspect there's a fair amount of personal preference in this.

When I first started writing Objective-C, well, at least once I really started to grok the language, I got into the habit of doing crazy nesting like your first example. Well, maybe not quite that crazy, but still fairly crazy ;) Then I started writing articles on Cocoa for MacTech magazine, and because the series was intended as an introduction to Cocoa for people unfamiliar with Objective-C (basically Carbon / Mac Toolbox programmers), I needed to make the code as clear and easy to understand as possible, so I started writing my code for the articles more like your second example to avoid confusion and there's no doubt the code was easier to read. Nowadays, my style is probably somewhere between your two examples, and I'm sure I'm not completely consistent about it. There are times when it makes sense to nest calls, times when it can cause you problems, and a great many situations where it probably just doesn't matter.

I almost never, for example, break out calls to alloc and init into two lines. Sometimes I'll even throw in an autorelease (if appropriate, of course) right on the line. When grabbing a reference to a singleton, I'll typically nest the call to the accessor (e.g. [NSFileHandle defaultFileHandle] in another call unless I'm going to be making many calls on the singleton object. With few exceptions (such as the alloc/init/autorelease and some building of NSString using formats, I don't usually nest more than two message calls. But, as I said, I'm not 100% consistent. I just do what feels right to me and sometimes I'll write it one way and then change it later.

As a practical matter, all that your first example really gets you is some vertical space on the page and possibly a meaningless amount of memory to hold a few pointers (I'm not even sure about that - I don't know much about compiler optimizations on modern processors, it could be those chunks of code compile to the exact same end result but even if not, a few pointers on a modern machine is meaningless). The downside to the first example is that it makes the code harder to read and can be much, much harder to debug. When you set a breakpoint at that line of code, for example, it will appear to stay at the same line through several instructions as you step through your code, and if it breaks during one of those nested calls, you'll have a harder time discerning which command it was on when it broke than if they were broken out.

I'd say, it's best to follow Wil's advice on this one (Shakespeare, not Shipley, although the latter probably has an opinion on the matter) - and be true to thine own self. Or, would that be YES to tine own self? Sorry, didn't get much sleep last night. I'll shut up now.

Peace,
Jeff
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to