Class CyclomaticComplexityCheck

All Implemented Interfaces:
Configurable, Contextualizable

Checks cyclomatic complexity against a specified limit. It is a measure of the minimum number of possible paths through the source and therefore the number of required tests, it is not about quality of code! It is only applied to methods, c-tors, static initializers and instance initializers.

The complexity is equal to the number of decision points + 1. Decision points:

  • if, while, do, for, ?:, catch, switch, case statements.
  • Operators && and || in the body of target.
  • when expression in case labels, also known as guards.

By pure theory level 1-4 is considered easy to test, 5-7 OK, 8-10 consider re-factoring to ease testing, and 11+ re-factor now as testing will be painful.

When it comes to code quality measurement by this metric level 10 is very good level as a ultimate target (that is hard to archive). Do not be ashamed to have complexity level 15 or even higher, but keep it below 20 to catch really bad-designed code automatically.

Please use Suppression to avoid violations on cases that could not be split in few methods without damaging readability of code or encapsulation.

Parent is com.puppycrawl.tools.checkstyle.TreeWalker

Violation Message Keys:

  • cyclomaticComplexity
Since:
3.2
  • Field Details

  • Constructor Details

  • Method Details

    • setSwitchBlockAsSingleDecisionPoint

      public void setSwitchBlockAsSingleDecisionPoint(boolean switchBlockAsSingleDecisionPoint)
      Setter to control whether to treat the whole switch block as a single decision point.
      Parameters:
      switchBlockAsSingleDecisionPoint - whether to treat the whole switch block as a single decision point.
      Since:
      6.11
    • setMax

      public final void setMax(int max)
      Setter to specify the maximum threshold allowed.
      Parameters:
      max - the maximum threshold
      Since:
      3.2
    • getDefaultTokens

      public int[] getDefaultTokens()
      Description copied from class: AbstractCheck
      Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.
      Specified by:
      getDefaultTokens in class AbstractCheck
      Returns:
      the default tokens
      See Also:
    • getAcceptableTokens

      public int[] getAcceptableTokens()
      Description copied from class: AbstractCheck
      The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.
      Specified by:
      getAcceptableTokens in class AbstractCheck
      Returns:
      the token set this check is designed for.
      See Also:
    • getRequiredTokens

      public final int[] getRequiredTokens()
      Description copied from class: AbstractCheck
      The tokens that this check must be registered for.
      Specified by:
      getRequiredTokens in class AbstractCheck
      Returns:
      the token set this must be registered for.
      See Also:
    • visitToken

      public void visitToken(DetailAST ast)
      Description copied from class: AbstractCheck
      Called to process a token.
      Overrides:
      visitToken in class AbstractCheck
      Parameters:
      ast - the token to process
    • leaveToken

      public void leaveToken(DetailAST ast)
      Description copied from class: AbstractCheck
      Called after all the child nodes have been process.
      Overrides:
      leaveToken in class AbstractCheck
      Parameters:
      ast - the token leaving
    • visitTokenHook

      private void visitTokenHook(DetailAST ast)
      Hook called when visiting a token. Will not be called the method definition tokens.
      Parameters:
      ast - the token being visited
    • leaveMethodDef

      private void leaveMethodDef(DetailAST ast)
      Process the end of a method definition.
      Parameters:
      ast - the token representing the method definition
    • incrementCurrentValue

      private void incrementCurrentValue(BigInteger amount)
      Increments the current value by a specified amount.
      Parameters:
      amount - the amount to increment by
    • pushValue

      private void pushValue()
      Push the current value on the stack.
    • popValue

      private void popValue()
      Pops a value off the stack and makes it the current value.
    • visitMethodDef

      private void visitMethodDef()
      Process the start of the method definition.