if (grass_is_green) { ... }
vs if (grass_is_green != 0) { ... }
or if (grass_is_green == 1) { ... }
The one with the implit comparison to zero is more readable if (grass_is_green) { ... }
vs if (grass_is_green != 0) { ... }
or if (grass_is_green == 1) { ... }
The one with the implit comparison to zero is more readableif (bool_name == TRUE)
There is no ambiguity, and it forces you to handle exactly equal to 1 and not equal to zero separately.
if (bool_name == TRUE)
and
if (bit_field_value > 0)
are logically different even if they can be simplified to the same thing.
The worst part is that sometimes these redundancies grow and combine to create even more confounding expressions, frequently resulting in monsters resembling this:
if ( ((((!(var)!= FALSE)) != TRUE)) )
{
return FALSE;
}
else
{
return TRUE;
}The better approach is to always compare against FALSE, because that avoids the ambiguity of == TRUE only catching a single truthy value out of the millions of possible truthy values, contrary to the well-known semantics of C.
If you really need to distinguish between 1 and other truthy values, then just compare against 1 explicitly. Don't muddle the concept of truthiness.
The reason is for writing embedded code that doesn't depend on a specific compiler or architecture, which is important when code that was written in the 80s is still getting used everyday in new safety critical systems, where any kind of standard libraries are forbidden.
I agree it's not the best but it's definitely not uncommon.
The comp.lang.c FAQ touches upon this:
if (isupper(c) != 0) if (isupper(c) != 0)
This code is illegible to me. How do you read it aloud? "if c is upper is not zero" ? It makes no sense. Compare it with the normal way of writing "if(isupper(c))" which is pronounced "if c is upper", which is perfectly clear, grammatical English.The other two leave the reader with the lingering suspicion that the variable takes on two or more values. The latter more so than the former.