SimplifyBooleanExpression

Since Checkstyle 3.0

Description

Checks for over-complicated boolean expressions. Currently, it finds code like if (b == true), b || true, !false, boolean a = q > 12 ? true : false, etc.

Rationale: Complex boolean logic makes code hard to understand and maintain.

Examples

To configure the check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="SimplifyBooleanExpression"/>
  </module>
</module>
        

Example:

public class Test {

  public void bar() {

    boolean a, b;
    Foo c, d, e;

    if (!false) {};       // violation, can be simplified to true

    if (a == true) {};    // violation, can be simplified to a
    if (a == b) {};       // OK
    if (a == false) {};   // violation, can be simplified to !a
    if (!(a != true)) {}; // violation, can be simplified to a

    e = (a || b) ? c : d;     // OK
    e = (a || false) ? c : d; // violation, can be simplified to a
    e = (a && b) ? c : d;     // OK

    int s = 12;
    boolean m = s > 1 ? true : false; // violation, can be simplified to s > 1
    boolean f = c == null ? false : c.someMethod(); // OK
  }

}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.coding

Parent Module

TreeWalker