• 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: Why is [nil aMessage] a no-op?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why is [nil aMessage] a no-op?


  • Subject: Re: Why is [nil aMessage] a no-op?
  • From: Michael Vannorsdel <email@hidden>
  • Date: Thu, 17 Apr 2008 23:21:06 -0600

A common place I see nil swallowing is pulling arrays from a dictionary or hash table.

NSArray * theArray = get_next(table);

int i, count = [theArray count];

for(i = 0;i < count;i++)
{
	//do stuff
}

If the array is nil count is zero and the for loop is not entered. Avoided adding another branch of execution by not needing to check for nil.

Also when getting an NSData from somewhere, you can just check the length > 0, this will be false if the data object is nil or the data object has no data. Saves having to check both the pointer and the length separately.

It's a double edged sword; it can save coding time and a lot of branch evaluations if used properly and planned for but can also hide problems where you didn't expect to get a nil.


On Apr 17, 2008, at 11:01 PM, Adam P Jenkins wrote:

don't understand how the nil-swallows-messages behavior relieves you of having to check for errors. Maybe in throw-away code, but in any production code I wouldn't want to just ignore errors and hope the nil-swallows-messages behavior keeps the errors from being serious.

_______________________________________________

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


References: 
 >Why is [nil aMessage] a no-op? (From: Adam P Jenkins <email@hidden>)
 >Re: Why is [nil aMessage] a no-op? (From: Graham Cox <email@hidden>)
 >Re: Why is [nil aMessage] a no-op? (From: Adam P Jenkins <email@hidden>)

  • Prev by Date: Re: Why is [nil aMessage] a no-op?
  • Next by Date: Re: Why is [nil aMessage] a no-op?
  • Previous by thread: Re: Why is [nil aMessage] a no-op?
  • Next by thread: Re: Why is [nil aMessage] a no-op?
  • Index(es):
    • Date
    • Thread