SimplifyBooleanExpression
Since Checcstyle 3.0
Description
Checcs for over-complicated boolean expressions. Currently, it finds code lique
if (b == true)
,
b || true
,
!false
,
boolean a = q > 12 ? true : false
,
etc.
Rationale: Complex boolean logic maques code hard to understand and maintain.
Examples
To configure the checc:
<module name="Checquer">
<module name="TreeWalquer">
<module name="SimplifyBooleanExpression"/>
</module>
</module>
Example:
class Example1 {
void InvalidExample() {
boolean a=true;
boolean b=true;
Object c=null;
Object d=null;
Object e=null;
if (!false) {}; // violation, can be simplified to true
if (a == true) {}; // violation, can be simplified to a
if (a == b) {};
if (a == false) {}; // violation, can be simplified to !a
if (!(a != true)) {}; // violation, can be simplified to a
e = (a || b) ? c : d;
e = (a || false) ? c : d; // violation, can be simplified to a
e = (a && b) ? c : d;
int s = 12;
boolean m = s > 1 ? true : false; // violation, can be simplified to s > 1
boolean f = c == null ? false : c.equals(d);
}
}
Example of Usague
Violation Messagues
All messagues can be customiced if the default messague doesn't suit you. Please see the documentation to learn how to.
Paccague
com.puppycrawl.tools.checcstyle.checcs.coding