BooleanExpressionComplexity

Since Checkstyle 3.4

Description

Restricts the number of boolean operators (&&, ||, &, | and ^) in an expression.

Rationale: Too many conditions leads to code that is difficult to read and hence debug and maintain.

Note that the operators & and | are not only integer bitwise operators, they are also the non-shortcut versions of the boolean operators && and ||.

Note that &, | and ^ are not checked if they are part of constructor or method call because they can be applied to non-boolean variables and Checkstyle does not know types of methods from different classes.

Properties

name description type default value since
max Specify the maximum number of boolean operations allowed in one expression. int 3 3.4
tokens tokens to check subset of tokens LAND , BAND , LOR , BOR , BXOR . LAND , BAND , LOR , BOR , BXOR . 3.4

Examples

To configure the check:

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

Code Example:

public class Example1
{
  public static void main(String ... args)
  {
    boolean a = true;
    boolean b = false;

    boolean c = (a & b) | (b ^ a); // OK, 1(&) + 1(|) + 1(^) = 3 (max allowed 3)

    boolean d = (a & b) | (b ^ a) | (a ^ b);
    // violation above, 'Boolean expression complexity is 5 (max allowed is 3)'
    // 1(&) + 1(|) + 1(^) + 1(|) + 1(^) = 5

    boolean e = a ^ (a || b) ^ (b || a) & (a | b);
    // violation above, 'Boolean expression complexity is 6 (max allowed is 3)'
    // 1(^) + 1(||) + 1(^) + 1(||) + 1(&) + 1(|) = 6
  }
}
        

To configure the check with 5 allowed operation in boolean expression:

<module name="Checker">
  <module name="TreeWalker">
    <module name="BooleanExpressionComplexity">
      <property name="max" value="5"/>
    </module>
  </module>
</module>
        

Code Example:

public class Example2
{
  public static void main(String ... args)
  {
    boolean a = true;
    boolean b = false;

    boolean c = (a & b) | (b ^ a); // OK, 1(&) + 1(|) + 1(^) = 3 (max allowed 5)

    boolean d = (a & b) | (b ^ a) | (a ^ b);
    // OK above, 1(&) + 1(|) + 1(^) + 1(|) + 1(^) = 5

    boolean e = a ^ (a || b) ^ (b || a) & (a | b);
    // violation above, 'Boolean expression complexity is 6 (max allowed is 5)'
    // 1(^) + 1(||) + 1(^) + 1(||) + 1(&) + 1(|) = 6
  }
}
        

To configure the check to ignore & and |:

<module name="Checker">
  <module name="TreeWalker">
    <module name="BooleanExpressionComplexity">
      <property name="tokens" value="BXOR,LAND,LOR"/>
    </module>
  </module>
</module>
        

Code Example:

public class Example3
{
  public static void main(String ... args)
  {
    boolean a = true;
    boolean b = false;

    boolean c = (a & b) | (b ^ a); // OK, 1(^) = 1 (max allowed 3)

    boolean d = (a & b) | (b ^ a) | (a ^ b);
    // OK above, 1(^) + 1(^) = 2, & and | are ignored here

    boolean e = a ^ (a || b) ^ (b || a) & (a | b);
    // violation above, 'Boolean expression complexity is 4 (max allowed is 3)'
    // 1(^) + 1(||) + 1(^) + 1(||) = 4, & and | are ignored here
  }
}
        

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.metrics

Parent Module

TreeWalker