t0 wrote:
Quote:
I am complaining about comparing boolean for equality to boolean to get a boolean result. Say it like that, and it sounds pretty obscure, no?
I think I prefer to compare the results of functions (even in the case where they return boolen values) as a defensive programming mechanism. If someone else changes the function to return a different type and misses one or more of the references, hopefully my compiler will generator a warning indicating such.
Yes - the condition in an "if" is required to be boolean (in any sensible language). I prefer to make it boolean, rather than have an error reported.
The problem with a language that does NOT require a condition to be a true boolean type, is that people introduce horribly difficult bugs, by doing what you suggest:
Code:
#define TRUE 1
if (SillyCFunction() == TRUE) { NeverDoThis(); }
This, where SillyCFunction is perfectly within its rights to return zero for false, and any other value for true. That is what a conditional will test for, by definition of the language.
Arbitrarily adding a layer of text that you feel "looks better" is just making the code more verbose, less clear, and prone to an additional set of bugs.
The only valid way of coding the above sort of C thing would be:
Code:
#define FALSE (0)
if (SillyCFunction() != FALSE) { DoThisWentTrue(); }
I still wouldn't use the above.
If I really have to have a FALSE constant, and even a TRUE one, I will use:
Code:
#define FALSE (0 != 0)
#define TRUE (!FALSE)
which is as language portable as it can be.
_________________
"Striking up conversations with strangers is an autistic person's version of extreme sports."
Kamran Nazeer