C & C++

C & C++

Made by DeepSource

Boolean expression can be simplified CXX-C2015

Anti-pattern
Minor

Boolean expressions involving boolean constants can be simplified to use the appropriate boolean expression directly, using DeMorgan's Theorem. This helps in improving code readability and reducing unnecessary complexity.

Bad Practice

if (b == true); // can be simplified to if (b)

if (b == false); // can be simplified to if (!b)

if (b && true); // can be simplified to if (b)

if (b && false); // can be simplified to if (false)

if (b || true); // can be simplified to if (true)

if (b || false); // can be simplified to if (b)

e ? true : false; // can be simplified to e

e ? false : true; // can be simplified to !e

if (true) t(); else f(); // can be simplified to t();

if (!a || !b); // can be simplified to if (a && b);

Recommended

if (b) // simplified form

if (!b) // simplified form

if (b) // simplified form

if (false) // simplified form

if (true) // simplified form

if (b) // simplified form

e // simplified form

!e // simplified form

t(); // simplified form

if (a && b); // simplified