I keep seeing code that does checks like this
if (IsGood == false)
{
DoSomething();
}
or this
if (IsGood == true)
{
D
I prefer !IsGood
because to me, it is more clear and consise. Checking if a boolean == true
is redundant though, so I would avoid that. Syntactically though, I don't think there is a difference checking if IsGood == false
.
I agree with you (and am also annoyed by it). I think it's just a slight misunderstanding that IsGood == true
evaluates to bool
, which is what IsGood
was to begin with.
I often see these near instances of SomeStringObject.ToString()
.
That said, in languages that play looser with types, this might be justified. But not in C#.
If you really think you need:
if (Flag == true)
then since the conditional expression is itself boolean you probably want to expand it to:
if ((Flag == true) == true)
and so on. How many more nails does this coffin need?
In many languages, the difference is that in one case, you are having the compiler/interpreter dictate the meaning of true or false, while in the other case, it is being defined by the code. C is a good example of this.
if (something) ...
In the above example, "something" is compared to the compiler's definition of "true." Usually this means "not zero."
if (something == true) ...
In the above example, "something" is compared to "true." Both the type of "true" (and therefor the comparability) and the value of "true" may or may not be defined by the language and/or the compiler/interpreter.
Often the two are not the same.
If you happen to be working in perl you have the option of
unless($isGood)
The technique of testing specifically against true or false is definitely bad practice if the variable in question is really supposed to be used as a boolean value (even if its type is not boolean) - especially in C/C++. Testing against true
can (and probably will) lead to subtle bugs:
These apparently similar tests give opposite results:
// needs C++ to get true/false keywords
// or needs macros (or something) defining true/false appropriately
int main( int argc, char* argv[])
{
int isGood = -1;
if (isGood == true) {
printf( "isGood == true\n");
}
else {
printf( "isGood != true\n");
}
if (isGood) {
printf( "isGood is true\n");
}
else {
printf( "isGood is not true\n");
}
return 0;
}
This displays the following result:
isGood != true
isGood is true
If you feel the need to test variable that is used as a boolean flag against true/false (which shouldn't be done in my opinion), you should use the idiom of always testing against false because false can have only one value (0
) while a true can have multiple possible values (anything other than 0
):
if (isGood != false) ... // instead of using if (isGood == true)
Some people will have the opinion that this is a flaw in C/C++, and that may be true. But it's a fact of life in those languages (and probably many others) so I would stick to the short idiom, even in languages like C# that do not allow you to use an integral value as a boolean.
See this SO question for an example of where this problem actually bit someone...