• 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: BOOL value in Dictionary
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: BOOL value in Dictionary


  • Subject: Re: BOOL value in Dictionary
  • From: "Clark Cox" <email@hidden>
  • Date: Fri, 21 Nov 2008 11:01:39 -0800

On Fri, Nov 21, 2008 at 7:54 AM, David Blanton <email@hidden> wrote:
> Why is :
>
> if ( boolVar == YES) or if ( boolVar == NO)
>
> bad form?

(boolVar == NO) is fine.
(boolVar == YES) is bad form (and could lead to incorrect results)


In C, any non-zero value evaluates to true in a boolean context, so
all of these if statements are equivalent to if(true):

if(YES)
if(42)
if(3.14159)

So it is entirely possible that you may encounter a "true" boolean
value that isn't equal to 1:

BOOL b = 42; //This is a true boolean, but it is not equal to YES

if(b == YES) //This is false
if(b) //This is true

So, the moral of the story is *never* compare a boolean value against
YES. At best it is redundant, and at worst it is incorrect.

That is, never write "if(b == YES)". Instead, it's best to write
"if(b)", "if(b != 0)" or "if(b != NO)"


--
Clark S. Cox III
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: 
 >Re: BOOL value in Dictionary (From: Heinrich Giesen <email@hidden>)
 >Re: BOOL value in Dictionary (From: David Blanton <email@hidden>)

  • Prev by Date: Re: NSCollectionView woes
  • Next by Date: Re: release and reference counting query
  • Previous by thread: Re: BOOL value in Dictionary
  • Next by thread: Re: BOOL value in Dictionary
  • Index(es):
    • Date
    • Thread