• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: is this badly written code?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: is this badly written code?


  • Subject: Re: is this badly written code?
  • From: Adam Leonard <email@hidden>
  • Date: Mon, 14 Apr 2008 23:09:00 -0700

Another approach if you are targeting 10.5 is to use the new dot syntax since the methods you call should be accessors. (Note that this all assumes that the accessors that use the dot syntax are declared and defined as returning specific types; i.e., mainWindowController returns an NSWindowController subclass that declares a treeController method. It would not work if mainWindowController returns id)

So,

NSManagedObject *selectedTreeObject = [[self delegate] mainWindowController.treeController.selectedObjects objectAtIndex:0]

That is not significantly shorter, but it saves you from counting all those brackets to make sure you have enough at the beginning (although Xcode will help you with that too)

I find my myself nesting lots of calls on a single line. It is faster to write, and I think easier to understand.
Paraphrasing Wil Shipley (I believe), variables are variable. They are supposed to change. If you are just creating a temporary variable to use only in the next step, it seems unnecessarily wasteful. Your variable names (as you see) end up being the same as the methods you call on that line.


Also, in most cases I don't think you should worry about one of the calls returning nil. If, for example, mainWindowController returns nil, then a call to nil will return nil, so selectedTreeObject will be nil by the end. So, a simple if(!selectedTreeObject) will work fine. The only disadvantage is you don't know which specific call failed, but in the majority of cases, that is not a problem in production code.

Now, if you find yourself asking for the selectedTreeObject many times in many different contexts, then that is a different story. There, I would figure out a way to get there with fewer calls, like adding a method to get it in the delegate's class.


Adam Leonard

On Apr 14, 2008, at 7: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 am looking for some guidance on best practices in a situation with a
lot of nested calls like this. If ultimately the only value I care
about is the final one, selectedTreeObject, whats the best way to go
about getting it? I know "best" is a subjective word. Interested to
hear all of your opinions.

Adam
_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: is this badly written code?
      • From: "Michael Ash" <email@hidden>
References: 
 >is this badly written code? (From: "Adam Gerson" <email@hidden>)

  • Prev by Date: Re: NSDateFormatter giving different results in different programs
  • Next by Date: Documents, Archived Objects, and Bindings
  • Previous by thread: Re: is this badly written code?
  • Next by thread: Re: is this badly written code?
  • Index(es):
    • Date
    • Thread