Re: if ([[NSUserDefaults preferences] check box]==true) ?
Re: if ([[NSUserDefaults preferences] check box]==true) ?
- Subject: Re: if ([[NSUserDefaults preferences] check box]==true) ?
- From: Alastair Houghton <email@hidden>
- Date: Sun, 9 Nov 2003 23:20:16 +0000
On 9 Nov 2003, at 11:18, Wade Tregaskis wrote:
>
> Also, if you are testing against true, *don't* use == to do it. C and
>
> most of the code written in it assumes that any non-zero value is
>
> true,
>
> so you can trip yourself up and end-up in a situation where something
>
> appears (to your code) to be neither true or false.
>
>
Perhaps we should apply some fuzzy logic to this, and just see if
>
things are > false. ;)
:-) That still wouldn't be right, of course, because -1 is just as
true as +1 (in C, at any rate).
>
Are you basing this on experience, or just the hypothetical problem?
>
I'm aware of this problem, but have never in my life run into it, not
>
even once.
I *have* seen a couple of inexperienced C programmers fall foul of
this. The reason it doesn't occur more often is that most C
programmers automatically write
if (x)
rather than
if (x == true)
because it's a standard C idiom.
One particular reason you might get caught-out is if you use the result
of a function/method that returns a boolean of some description (other
than "bool" _in_C++_, for which a conversion is forced), because a lot
of C programmers will just write
return <some expression>;
in their functions, where <some expression> may actually have a value
other than 0 or 1. This makes perfect sense to a C programmer, but it
isn't obvious that the function, which is probably prototyped
BOOL foo(<parameters>);
might return 3, which is every bit as true as TRUE (or YES), but
unfortunately isn't identically equal to TRUE.
The worst variant of this type of mistake I've seen is actually with
BASIC, rather than C, because BASIC typically only provides bitwise
boolean operators, so you can actually end-up with a value x for which
the following expressions are all (disturbingly) true:
x <> true
x <> false
x
not x
(x and true) = false (in implementations where true = 1)
Kind regards,
Alastair.
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.