home > resources > c++ coding standard > online version

Prefer positive boolean comparisons

It's much easier to think in a positive way about a situation than to be presented with the negative alternative and having to transform it in your mind by yourself to positive. People tend to have a 'logical' or 'the default behaviour' feeling about true, which makes it easy to think about. On the contrary, false is mostly regarded is the 'exception', 'the error situation' or the 'alternative way out'. Therefor we prefer constructs like this:

Example 6-3. Positive boolean comparison, the right way

setup();
if (true == something)
{
    dowork();
}
cleanup();
return;

above the following negative counterpart:

Example 6-4. Positive boolean comparison, the wrong way

setup();
if (false == something)
{
    cleanup();
    return;
}
dowork();
cleanup();
return;