scitools.com

← Metric catalog

Cyclomatic Complexity is one of the source-code metrics Understand computes across 17+ languages — browse them in the GUI, export and track them, or script them with the Python API.

Cyclomatic Complexity

API ID: Cyclomatic
Also Known As: CC
Languages: Ada, Assembly, Basic, C#, C++, Fortran, Java, Jovial, Pascal, Python, Rust, VHDL, Web
Targets: Files, Functions, Modules, Packages, Subprograms

McCabe Cyclomatic Complexity, the number of decision points + 1.

McCabe Cyclomatic complexity as per the original NIST paper on the subject. The cyclomatic complexity of any structured program with only one entrance point and one exit point is equal to the number of decision points contained in that program plus one. Understand counts the keywords for decision points (FOR, WHILE, etc) and then adds 1. For a switch statement, each 'case' is counted as 1. For languages with macros, the expanded macro text is also included in the calculation.

For example:


void cyclomaticDemo() {
  bool a = true, b = true, c = true;

  if (a || (b && c)) {
    while (a ? b : c) {
      for (int i = 0; i < 10; i++) {
        switch (i) {
          case 1:
          case 2:
            cout << i << endl;
            break;
          case 5:
            break;
          default:
            cout << i << endl;
        }
      }
    }
  } else {
    try {
      do {
        cout << a << b << c << endl;
      } while (a);
    }
    catch (...) {
    }
  }
}

if, while, the ?: conditional, for, do, and catch are always decision points, for 6 total. Each case in the switch statement adds another (3 total: case 1, case 2, case 5), and the &&/|| in the if condition are not counted. That's 9 decision points, for a Cyclomatic Complexity of 10.

To count the entire switch statement as a single decision instead of one per case, use Modified Cyclomatic Complexity. To also count && and || operators, use Strict Modified Cyclomatic Complexity. To count those operators without changing how the switch is handled, use Strict Cyclomatic Complexity.

See Sum Cyclomatic Complexity, Max Cyclomatic Complexity, and Average Cyclomatic Complexity for aggregated values, such as for an architecture or the project.

Also known as McCabe - Cyclomatic Complexity (CC).


Targets By Language: Configuration: