Size and Complexity Rules ("rulesets/size.xml")

AbcComplexity Rule

Calculates the ABC metric for methods/classes and checks against configured threshold values.

The maxMethodComplexity property holds the threshold value for the ABC complexity value (score) for each method. If this value is non-zero, a method with a complexity value greater than this value is considered a violation.

The maxClassAverageMethodComplexity property holds the threshold value for the average ABC complexity value (score) for each class. If this value is non-zero, a class with an average complexity value greater than this value is considered a violation.

This rule treats "closure fields" as methods. If a class field is initialized to a Closure (ClosureExpression), then that Closure is analyzed and checked just like a method.

Property Description Default Value
maxMethodComplexity The maximum ABC complexity value (score) allowed for a single
method (or "closure field"). If zero or null, then do not
check method-level complexity.
60
maxClassAverageMethodComplexity The maximum ABC complexity value (score) allowed for a class,
calculated as the average complexity of its methods or
"closure fields". If zero or null, then do not check
class-level complexity.
60
ignoreMethodNames Specifies one or more (comma-separated) method names that
that should not cause a rule violation. The names may
optionally contain wildcards (*,?). Note that the ignored
methods still contribute to the class complexity value.
null

ABC Size/Complexity Metric Calculation Rules

The ABC complexity value (score) is calculated as follows: The ABC metric measures size/complexity by counting the number of Assignments (A), Branches (B) and Conditions (C) and assigns a single numerical score calculated as:

|ABC| = sqrt((A*A)+(B*B)+(C*C))

The ABC Metric calculation rules for Groovy:

  • Add one to the assignment count for each occurrence of an assignment operator, excluding constant declarations: = *= /= %= += = = &= |= ^= >>>=
  • Add one to the assignment count for each occurrence of an increment or decrement operator (prefix or postfix): ++ --
  • Add one to the branch count for each function call or class method call.
  • Add one to the branch count for each occurrence of the new operator.
  • Add one to the condition count for each use of a conditional operator: == != = = = =~ ==~
  • Add one to the condition count for each use of the following keywords: else case default try catch ?
  • Add one to the condition count for each unary conditional expression.

Notes

  • See the ABC Metric specification
  • See the Blog post describing guidelines for interpreting an ABC score
  • See the GMetrics ABC metric. This includes a discussion of guidelines for interpreting ABC scores.
  • This rule requires Groovy 1.6 (or later).
  • This rule requires the GMetrics jar on the classpath. See GMetrics.

ClassSize Rule

Checks if the size of a class exceeds the number of lines specified by the maxLines property.

Property Description Default Value
maxLines The maximum number of lines allowed in a class definition. 1000

CyclomaticComplexity Rule

Calculates the Cyclomatic Complexity for methods/classes and checks against configured threshold values.

The maxMethodComplexity property holds the threshold value for the cyclomatic complexity value for each method. If this value is non-zero, a method with a cyclomatic complexity value greater than this value is considered a violation.

The maxClassAverageMethodComplexity property holds the threshold value for the average cyclomatic complexity value for each class. If this value is non-zero, a class with an average cyclomatic complexity value greater than this value is considered a violation.

This rule treats "closure fields" as methods. If a class field is initialized to a Closure (ClosureExpression), then that Closure is analyzed and checked just like a method.

Property Description Default Value
maxMethodComplexity The maximum cyclomatic complexity value allowed for a single
method (or "closure field"). If zero or null, then do not
check method-level complexity.
20
maxClassAverageMethodComplexity The maximum cyclomatic complexity value allowed for a class,
calculated as the average complexity of its methods or
"closure fields". If zero or null, then do not check
class-level complexity.
20
ignoreMethodNames Specifies one or more (comma-separated) method names that
that should not cause a rule violation. The names may
optionally contain wildcards (*,?). Note that the ignored
methods still contribute to the class complexity value.
null

Cyclomatic Complexity Metric Calculation Rules

The cyclomatic complexity value is calculated as follows:

Start with a initial (default) value of one (1). Add one (1) for each occurrence of each of the following:

  • if statement
  • while statement
  • for statement
  • case statement
  • catch statement
  • && and || boolean operations
  • ?: ternary operator and ?: Elvis operator.
  • ?. null-check operator

Notes

MethodSize Rule

Checks if the size of a method exceeds the number of lines specified by the maxLines property.

Property Description Default Value
maxLines The maximum number of lines allowed in a method definition. 100
ignoreMethodNames Specifies one or more (comma-separated) method names that
should be ignored (i.e., that should not cause a rule
violation). The names may optionally contain wildcards (*,?).
null

NestedBlockDepthRule Rule

Checks for blocks or closures nested more deeply than a configured maximum number. Blocks include if, for, while, switch, try, catch, finally and synchronized blocks/statements, as well as closures.

Property Description Default Value
maxNestedBlockDepth The maximum number of nesting levels. A block or closure nested
deeper than that number of levels is considered a violation.
5