Question

Looking at some code I noticed that another dev had changed every instance of true to !false. Why would you do that? thx

Was it helpful?

Solution

There is no good reason to write !false instead of true.
Bad reasons could include obfuscation (making the code harder to read), personal preferences, badly considered global search-and-replace, and shenanigans converting boolean values to integers.

It's possible that some confusion has been caused by the TRUE and FALSE definitions in Win32, which are not of bool type but ints, and which may trigger warnings when used in boolean statements. Mainly, anything non-zero is "true", but if you want to make sure that "true" is always one when using integers instead of booleans, you sometimes see shenanigans like this. It's still not a good reason ;-)

OTHER TIPS

I have no idea and I've been writing C++ for a while. I suspect whatever he reasons were they weren't good ones.

I think this is a historical thing. IIRC there was a problem on some C/C++ compilers knowing what the compiler would use to represent as true. You can know false is zero hence !false will be whatever the current ABI will have as true.

Generically this is redundant but there are a few special case where it is useful but I can't think of one.

It might be necessary if the line

#define true false

just happened to be somewhere in the headers :)

maybe he got so many true and false in his code and he wanted to reverse it easily, he can just use his editor's search and replace,

I do not know actually but may be TRUE is not 1 (any thing > 0) and FALSE is zero so "not false" = 1 so he will be in safe place

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top