gnustep-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [RFA]: BOOL coding standards (Was: Problemwith+numberWithBool:?)


From: Kazunobu Kuriyama
Subject: Re: [RFA]: BOOL coding standards (Was: Problemwith+numberWithBool:?)
Date: Mon, 09 Feb 2004 16:43:55 +0900
User-agent: Mozilla/5.0 (X11; U; Linux i686; ja-JP; rv:1.4) Gecko/20030624 Netscape/7.1

Nicola Pero wrote:

IOW: C "truths" (i.e. C truth semantice) aren't implicitly converted to BOOL YES/NO. The conversion must be made explict.

But who in his sane mind would write:


[NSNumber numberWithBool: (i=256)]; => NO

instead of:

    [NSNumber numberWithBool: (i=256)!=0]; => YES

?


It's easy to build more convincing examples.

If you follow the "BOOLs are the same as C truth values" philosophy
without thinking too much, then you could end up writing stuff like

- (BOOL) isNonEmpty
{
 return count;
}

where count is an int counting the number of items in an array.

You could think that since BOOLs work as C truth values, if count is > 0,
this is YES, if it's 0, it's NO (I'm sure you've seen enough C coders to
know they love being "clever" in that way with C truth values), so the
code should work, but unfortunately it doesn't.

Don't attribute it to C programmers. Sane C programmers don't (want to,
at least) do anything like above.  Apart from the insane, they never try
to devise a boolen type by themselves once they know the language well,
though they prefer defining convenience macros such as #define FALSE (0)
and #define TRUE (1) in their header file.  C99?  They cleverly leave
that type to the compiler!  We should know this makes a huge difference.

Apart from their being actually clever or not, it's responsibility for
Objective C how to define BOOL.  The fault lies only in the fact that BOOL
has not been made a built-in Objective C type so far (the spec defines it
as if it were, not explicitly stated like this though).  Without having
compilers exactly know the meaning of boolean, what can we expect them to
do with it?  They are only told BOOL is a kind of short integer!  Wrong
usage of BOOL must attribute to careless ObjC programmers, neither C
programmers nor compilers.  IOW, that's our problem.

(I'm not saying BOOL should be a built-in; this is another issue and I don't
want to discuss it here.)

Cheers,
- Kazunobu Kuriyama





reply via email to

[Prev in Thread] Current Thread [Next in Thread]