Re: !foo vs foo == nil
Re: !foo vs foo == nil
- Subject: Re: !foo vs foo == nil
- From: Negm-Awad Amin <email@hidden>
- Date: Thu, 21 Aug 2008 12:47:32 +0200
Am Do,21.08.2008 um 12:25 schrieb Thomas Engelmeier:
Am 21.08.2008 um 11:04 schrieb Negm-Awad Amin:
Sometimes you see in source code something like this:
if( booleanVarOrExpression == YES )
(I think, Rentzsch does it that way, IIRC) and Ithink, that this is
simply correct.
Oh, that is another discussion.
Yeah, but connected
It is antoher discussion in that way, that we have other types. (BOOL
vs. type of logical expression in C)
It is the same discussion in that way, that most programmers make some
assumptions about the value (and the casting).
(I bet, that 10 % believe, that the expression inside the if is typed
BOOL)
Doing this is absolutly correct.
BOOL var = …; // Something evaluating to YES or NO
if( var == YES )
a simple
BOOL var = …; // Something evaluating to YES or NO
if( var )
is a very good hidden implicit cast from BOOL to logical expression.
However I use the simple condition (without == YES), too – for
convenience.
BTW: This is an implicit cast, too:
BOOL var = a && b;
because the result of the right expression types to logical
expression, not to BOOL.
Bool != Boolean != bool, and sometimes that can introduce subtle
errors if it's not only used to evaluate logical statements but by
comparing values, especially to "YES", true and "TRUE".
Yes, and some people do calculations with it
andExpression = a * b, // of course in a more subtle context
orExpression = a + b; // argh …
In an large C++ Cross-Platform project I spotted some subtle
problems, IIRC from assignment of an bool to an Boolean for an API
that evaluated the values by comparison to "TRUE" or some code
assigned it to ints, giving different values for true (1 vs. 0xFF
vs. 0xFFFFFFFF). I don't know any more, fixed it, was glad my own
coding practices don't leave room for that problems, moved on ;-)...
:-)
Regards,
Tom_E
LG
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