• 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: !foo vs foo == nil
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: !foo vs foo == nil


  • Subject: Re: !foo vs foo == nil
  • From: Marcel Weiher <email@hidden>
  • Date: Wed, 20 Aug 2008 18:31:01 -0700


On Aug 20, 2008, at 6:14 PM, John C. Randolph wrote:
On Aug 20, 2008, at 5:04 PM, j o a r wrote:
On Aug 20, 2008, at 4:56 PM, John C. Randolph wrote:

Personally, I prefer "if (!foo)" over "if (foo == nil)", because the latter has the hazard of a typo that compiles. You can lose a fair bit of time staring at "if (foo = nil)" before you spot the mistake.


There is a GCC warning to help you with that, but in addition, you can learn to type:

	if (nil == foo)

Problem solved! :-)

...at the cost of five additional keystrokes, not to mention the aesthetics.

I was swayed by the "nil == foo" arguments for a while, but my gut kept tugging at me and now I have switched back to just if ( !foo), or better (I prefer positive ifs), if (foo). Of course, if all the if (foo) is protecting is a message-send, you can just leave it out altogether and let nil do its thing to messages...


I think what my gut was trying to tell me is that I don't really care about the value of the pointer itself, that is only an implementation detail of object references. So not only is if (foo) more concise, it is actually more intention-revealing than if ( nil==foo ). Oh, and it slices and dices, too..

Marcel

_______________________________________________

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: !foo vs foo == nil
      • From: Jim Correia <email@hidden>
References: 
 >!foo vs foo == nil (From: Torsten Curdt <email@hidden>)
 >Re: !foo vs foo == nil (From: Filip van der Meeren <email@hidden>)
 >Re: !foo vs foo == nil (From: Torsten Curdt <email@hidden>)
 >Re: !foo vs foo == nil (From: "John C. Randolph" <email@hidden>)
 >Re: !foo vs foo == nil (From: j o a r <email@hidden>)
 >Re: !foo vs foo == nil (From: "John C. Randolph" <email@hidden>)

  • Prev by Date: Re: !foo vs foo == nil
  • Next by Date: Re: Question about respondsToSelector
  • Previous by thread: Re: !foo vs foo == nil
  • Next by thread: Re: !foo vs foo == nil
  • Index(es):
    • Date
    • Thread