• 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: Negm-Awad Amin <email@hidden>
  • Date: Thu, 21 Aug 2008 10:37:21 +0200


Am Do,21.08.2008 um 09:54 schrieb Jules Colding:


On 21/08/2008, at 09.21, Thomas Davie wrote:


On 21 Aug 2008, at 09:06, Jules Colding wrote:


On 21/08/2008, at 01.56, John C. Randolph wrote:


On Aug 20, 2008, at 4:15 PM, Torsten Curdt wrote:

There was a common perception that NULL is not really the same as nil. But seems like in the end it really is (void*)0.


They differ in type, not in value.

"NULL" is (void *) 0.
"nil" is (id) 0.
"Nil" is (Class) 0.

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.

Which is why you should always write "if (nil == foo)".

Just to add my 2 cents to this discussion, I think there's something which hasn't been brought up (and I guess isn't often brought up by C programmers).


One of the two options doesn't make sense here.
Because of C's weak type system ! works on almost anything you throw at it. However, it is a *boolean* operator. Boolean negating a pointer is a hack that by happy coincidence works. On the other hand, nil == foo is type safe, and makes semantic sense.


For that simple reason, I'd go for nil == foo every time.

Yes, and in general you should always do "if (CONSTANT == foo)" to catch the potential "if (CONSTANT = foo)" error.
You should always compile with -Wall to catch potential
if( value1 = value2 )
even if value1 is no constant.

Cheers,
Amin



Best regards,
 jules

_______________________________________________

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

Amin Negm-Awad email@hidden




_______________________________________________

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: 
 >!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: Jules Colding <email@hidden>)
 >Re: !foo vs foo == nil (From: Thomas Davie <email@hidden>)
 >Re: !foo vs foo == nil (From: Jules Colding <email@hidden>)

  • Prev by Date: Re: Design Question: Pro & Cons of KVC/KVO
  • Next by Date: Re: !foo vs foo == nil
  • Previous by thread: Re: !foo vs foo == nil
  • Next by thread: Re: !foo vs foo == nil
  • Index(es):
    • Date
    • Thread