CyVis Software Metrics Collection, Analysis and Visualisation Tool

 

SourceForge.net Logo

What is Cyclomatic Complexity?

Cyclomatic complexity gives the number of paths that may be taken when a program is executed. Methods with a high Cyclomatic complexity tend to be more difficult to understand and maintain. Some of the tokens (in java) responsible for the program taking different paths during execution are:

  • while & do while statements.
  • If statements.
  • for statements.
  • Ternary Operators & Logical Operators.
  • Switch case statements.
  • Return, throw, throws, catch statement.

Example of Cyclomatic Complexity Calculation:

  public static int getGreatest(int a, int b)

  {
      return a>b?a:b;
      return a;

  }

The cyclomatic complexity for this method would be 3. Since the method has a ternary operator, it acts as one if and else, which in turn adds 2 to the cyclomatic complexity. We then add one to the cyclomatic complexity (raising it to 3) to denote the default path (in this case both if and else failing and the second return statement being executed).