You should never compare booleans with anything in any of the C languages. The right way to do this is to use either:
if (b)
or
if (!b)
This makes your code more readable (especially if you use intelligently named variables and functions like isPrime(n) or childThreadHasFinished ) and are safe. The reason is something like:
if (b == TRUE)
is not so safe that in fact there are a large number of b values ββthat will be evaluated as true, and TRUE is only one of them.
Consider the following:
#define FALSE 0 #define TRUE 1 int flag = 7; if (flag) printf ("number 1\n"); if (flag == TRUE) printf ("number 2\n");
You should get both of these lines printed if they work as expected, but you only get the first one. This is because 7 is truly true if handled correctly (0 is false, everything else is true), but an explicit test for equality is evaluated as false.
Update:
In response to your comment, that you thought that this would be more than the stupidity of the encoder: yes, there is (but I still will not reduce the stupidity of the encoder as a good enough reason - defensive programming is always a good idea).
I also mentioned readability, which is pretty high on my list of desired features in the code.
The condition must be either a comparison between objects or a flag (including boolean return values):
if (a == b) ... if (c > d) ... if (strcmp (e, "Urk") == 0) ... if (isFinished) ... if (userPressedEsc (ch)) ...
If you use (what I consider) an abomination like:
if (isFinished == TRUE) ...
where do you stay:
if (isFinished == TRUE) ... if ((isFinished == TRUE) == TRUE) ... if (((isFinished == TRUE) == TRUE) == TRUE) ...
etc.
The right way to do this for readability is to simply use the appropriate flag variable names.
paxdiablo
source share