Re: BOOL value in Dictionary
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