Re: RFC: booleans and the kernel

Jamie Lokier (lk@tantalophile.demon.co.uk)
Sat, 26 Jan 2002 03:08:41 +0000


Helge Hafting wrote:
> Why would anyone want to write if (X==false) or if (X==true) ?
> It is the "beginner's mistake" way of writing code. Then people learn,
> and write if (X) or if (!X). Comparing to true/false is silly.
> Nobody writes if ( (a==b) == true) so why do it in the simpler cases?

I usually without the == in these cases:

if (pointer) // test for non-0.
if (condition)
if (condition-valued-variable)

but not in these (although I am not very consistent):

if (integer != 0)
if (char != 0)

When using bool, I'm happy to write "if (X)" where X is a truth value
indicating a condition that has been tested, but if X were used as an
enumeration of truth values e.g. as in a theorem prover or a logic
simulator, I would tend to write ==, for example:

if (X == true && ptr && *ptr > 1)

The point being to illustrate the intent of the test (i.e. is it a
boolean test or a comparison against a point in a range of values), not
simply for it to be semantically correct.

Just to break that rule, however, if p were a pointer and x were an
integer, I would write:

x = (p != 0);

rather than

x = p;

:-)

enjoy,
-- Jamie
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/