Skip to contentSkip to conversation

RFC: booleans and the kernel

DISCUSSION Helge Hafting 4 participants 6 messages Jan 25, 2002 – Jan 27, 2002

lore
Share & tools
lore
#0 lore raw
Quoted 7 lines · Jeff Garzik
Quoted 5 lines · Oliver Xymoron
Quoted 3 lines · Jeff Garzik

Where variables are truly boolean use of a bool type makes the intentions of the code more clear. And it also gives the compiler a slightly better chance to optimize code [I suspect].

Quoted 6 lines
Quoted 2 lines

Unlikely. The compiler can already figure this sort of thing out from context.

X, true, and false are of type int.
If one tests X==false and then later on tests X==true, how does the compiler know the entire domain has been tested? With a boolean, it

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?

Quoted 1 line

would. Or a switch statement... if both true and false are covered,

A switch statement on a boolean value is stupid. Use if - there is only two cases.

Helge Hafting

#1 lore raw
Quoted 4 lines · Helge Hafting

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

#2 lore raw
In reply to Helge Hafting
Quoted 9 lines · Jamie Lokier
Quoted 4 lines · Helge Hafting

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.

Huh? I thought we were talking about C here, // in C is an abomination, use /* */ :-)

Quoted 4 lines

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

  x = (p != 0);

Heard about NULL ?

Quoted 3 lines

rather than

  x = p;

Because that would give a compile error...

Signature · 9 lines
Mark Zealey
[email protected]
[email protected]

UL++++>$ G!>(GCM/GCS/GS/GM) dpu? s:-@ a16! C++++>$ P++++>+++++$ L+++>+++++$
!E---? W+++>$ N- !o? !w--- O? !M? !V? !PS !PE--@ PGP+? r++ !t---?@ !X---?
!R- b+ !tv b+ DI+ D+? G+++ e>+++++ !h++* r!-- y--

(www.geekcode.com)
#3 lore raw
In reply to Helge Hafting
Quoted 6 lines · Jamie Lokier
Quoted 4 lines · Helge Hafting

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?

Quoted 7 lines

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):

Khmmm please enlighten me ...

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

Why? Simply use for example type 'char' as boolean value. Let's say 0 means false and other value is true.

So:

if (x) printf("true");

or

if (!x) printf("false");

Why do you want to overcomplicate?

Even:

x=a>b;
if (x) printf("A is greater than B");

ONE thing which is best in C is the less strictly type rules eg you can use 'char' to store eg c='A' or c=2.
Hey guys, C was designed to write an OS it's not something other ...

- Gábor

#4 lore raw
In reply to Helge Hafting
Quoted 7 lines · Gábor Lénárt

Khmmm please enlighten me ...

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

Why? Simply use for example type 'char' as boolean value. Let's say 0 means false and other value is true.

Quoted 7 lines

So:

if (x) printf("true");
or
if (!x) printf("false");

Why do you want to overcomplicate?

If the variable holds a boolean in the C language, fair enough but if it's being used as a range in a truth-value system of _another_ language, i.e. it simply _represents_ a truth value, I would write it differently.

If it were a theorem proving paper, the different kinds of variable would have a different font or colour :-)

Quoted 5 lines

x=a>b;
if (x) printf("A is greater than B");

ONE thing which is best in C is the less strictly type rules eg you can use 'char' to store eg c='A' or c=2.

You seem to have missed the point. We _know_ the C language rules. I agree that non-strict typeing is quite useful, although C is in fact quite strict. Lisp has far less strict typing :-)

Quoted 1 line

Hey guys, C was designed to write an OS it's not something other ...

Perhaps, but it's pretty useful for something other.

-- Jamie

#5 lore raw
In reply to Helge Hafting
Quoted 6 lines · Mark Zealey
Quoted 4 lines

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

  x = (p != 0);

Heard about NULL ?

I prefer 0, thanks. NULL has header file portability problems.

-- Jamie