ClassFanOutComplexity

Since Checkstyle 3.4

Description

Checks the number of other types a given class/record/interface/enum/annotation relies on. Also, the square of this has been shown to indicate the amount of maintenance required in functional programs (on a file basis) at least.

This check processes files in the following way:

  1. Iterates over all tokens that might contain type reference.
  2. If a class was imported with direct import (i.e. import java.math.BigDecimal), or the class was referenced with the package name (i.e. java.math.BigDecimal value) and the package was added to the excludedPackages parameter, the class does not increase complexity.
  3. If a class name was added to the excludedClasses parameter, the class does not increase complexity.

Properties

name description type default value since
excludeClassesRegexps Specify user-configured regular expressions to ignore classes. Pattern[] ^$ 7.7
excludedClasses Specify user-configured class names to ignore. String[] ArrayIndexOutOfBoundsException, ArrayList, Boolean, Byte, Character, Class, Collection, Deprecated, Deque, Double, DoubleStream, EnumSet, Exception, Float, FunctionalInterface, HashMap, HashSet, IllegalArgumentException, IllegalStateException, IndexOutOfBoundsException, IntStream, Integer, LinkedHashMap, LinkedHashSet, LinkedList, List, Long, LongStream, Map, NullPointerException, Object, Optional, OptionalDouble, OptionalInt, OptionalLong, Override, Queue, RuntimeException, SafeVarargs, SecurityException, Set, Short, SortedMap, SortedSet, Stream, String, StringBuffer, StringBuilder, SuppressWarnings, Throwable, TreeMap, TreeSet, UnsupportedOperationException, Void, boolean, byte, char, double, float, int, long, short, var, void 5.7
excludedPackages Specify user-configured packages to ignore. String[] {} 7.7
max Specify the maximum threshold allowed. int 20 3.4

Examples

To configure the check:


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

Example1:

The check passes without violations in the following:


class Example1 {
  Set set = new HashSet();   // ok, Set and HashSet are ignored
  Map map = new HashMap();   // ok, Map and HashMap are ignored
  Date date = new Date();
  Time time = new Time();
  Place place = new Place();
  int value = 10;            // ok, int is ignored
  BufferedReader br;
  File file;

  void method() {
    var result = "result";   // ok, var is ignored
  }
}

To configure the check with a threshold of 2:


<module name="Checker">
  <module name="TreeWalker">
    <module name="ClassFanOutComplexity">
      <property name="max" value="2"/>
    </module>
  </module>
</module>

Example2:

The check results in a violation in the following:


class Example2 { // violation, 'Class Fan-Out Complexity is 5 (max allowed is 2)'
  Set set = new HashSet();   // ok, Set and HashSet are ignored
  Map map = new HashMap();   // ok, Map and HashMap are ignored
  Date date = new Date();
  Time time = new Time();
  Place place = new Place();
  int value = 10;            // ok, int is ignored
  BufferedReader br;
  File file;

  void method() {
    var result = "result";   // ok, var is ignored
  }
}

To configure the check with three excluded classes HashMap, HashSet and Place3:


<module name="Checker">
  <module name="TreeWalker">
    <module name="ClassFanOutComplexity">
      <property name="excludedClasses" value="HashMap, HashSet, Place"/>
      <property name="max" value="3"/>
    </module>
  </module>
</module>

Example3:

The check results in a violation in the following:


class Example3 { // violation 'Class Fan-Out Complexity is 7 (max allowed is 3)'
  Set set = new HashSet();
  Map map = new HashMap();
  Date date = new Date();
  Time time = new Time();
  Place place = new Place();
  int value = 10;            // ok, int is ignored
  BufferedReader br;
  File file;

  void method() {
    var result = "result";   // ok, var ignored
  }
}

To configure the check to exclude classes with a regular expression .*Reader$:


<module name="Checker">
  <module name="TreeWalker">
    <module name="ClassFanOutComplexity">
      <property name="excludeClassesRegexps" value=".*Reader$"/>
      <property name="max" value="3"/>
    </module>
  </module>
</module>

Example4:

The check results in a violation in the following:


class Example4 { // violation 'Class Fan-Out Complexity is 4 (max allowed is 3)'
  Set set = new HashSet();   // ok, Set and HashSet are ignored
  Map map = new HashMap();   // ok, Map and HashMap are ignored
  Date date = new Date();
  Time time = new Time();
  Place place = new Place();
  int value = 10;            // ok, primitive types are ignored
  BufferedReader br;         // ok, Reader is excluded
  File file;

  void method() {
    var result = "result";   // ok, var is ignored
  }
}

To configure the check with an excluded package java.io:


<module name="Checker">
  <module name="TreeWalker">
    <module name="ClassFanOutComplexity">
      <property name="excludedPackages" value="java.io"/>
      <property name="max" value="5"/>
    </module>
  </module>
</module>

Example5:

The check passes without violations in the following:


class Example5 {
  Set set = new HashSet();   // ok, Set and HashSet are ignored
  Map map = new HashMap();   // ok, Map and HashMap are ignored
  Date date = new Date();
  Time time = new Time();
  Place place = new Place();
  int value = 10;            // ok, primitive types are ignored
  BufferedReader br;         // ok, BufferedReader is ignored
  File file;                 // ok, File is ignored

  void method() {
    var result = "result";   // ok, var is ignored
  }
}

Override property excludedPackages to mark some packages as excluded. Each member of excludedPackages should be a valid identifier:

  • java.util - valid, excludes all classes inside java.util, but not from the subpackages.
  • java.util. - invalid, should not end with a dot.
  • java.util.* - invalid, should not end with a star.

Note, that checkstyle will ignore all classes from the java.lang package and its subpackages, even if the java.lang was not listed in the excludedPackages parameter.

Also note, that excludedPackages will not exclude classes, imported via wildcard (e.g. import java.math.*). Instead of wildcard import you should use direct import (e.g. import java.math.BigDecimal).

Also note, that checkstyle will not exclude classes within the same file even if it was listed in the excludedPackages parameter. For example, assuming the config is


<module name="Checker">
  <module name="TreeWalker">
    <module name="ClassFanOutComplexity">
      <property name="excludedPackages" value="java.util"/>
      <property name="max" value="3"/>
    </module>
  </module>
</module>

Example6:

And the file is:


class Example6 { // violation 'Class Fan-Out Complexity is 4 (max allowed is 3)'
  Set set = new HashSet();   // ok, Set and HashSet are ignored
  Map map = new HashMap();   // ok, Map and HashMap are ignored
  Date date = new Date();    // ok, java.util package is ignored
  Time time = new Time();
  Place place = new Place();
  int value = 10;            // ok, primitive types are ignored
  BufferedReader br;
  File file;

  void method() {
    var result = "result";   // ok, var is ignored
  }
}

The Set, Map, HashMap, HashSet, Date member will not be counted, since the java.util added to the excludedPackages. The BufferReader, File member will be counted,

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.

Fully Qualified Name

com.puppycrawl.tools.checkstyle.checks.metrics.ClassFanOutComplexityCheck

Use this fully qualified class name in configuration when an exact class reference is required.

Parent Module

TreeWalker