How is Cyclomatic Complexity calculated?
  • 04 Mar 2022
  • 1 Minute to read
  • Contributors
  • Dark
    Light
  • PDF

How is Cyclomatic Complexity calculated?

  • Dark
    Light
  • PDF

Article Summary

Cyclomatic complexity in CodeScan is calculated by the number of decision points in a method.

For example, the below method would have a cyclomatic complexity of 5.

public void newMethod() {
     for(Integer i = 0; i < 100; i++){
      if (a == b && b == c){
        doStuff();
      } else if(a != b) {
        doOtherStuff();
      }
     }
   }

This counts:

  • The entry to the method
  • The entry to the for loop
  • The if statement and the first conditional
  • The second conditional in the if statement
  • The elseif statement

This does not include:

  • else statements
  • when else in switch statements

The cyclomatic complexity for a class will be the average complexity between all methods in that class.


Was this article helpful?