Skip to main content
Cyclomatic complexity is McCabe’s count of the number of linearly independent paths through a function’s control-flow graph. A function with no branches has complexity 1. Each if, for, while, case, boolean && / ||, ternary, or exception handler increments the count.

What mehen emits

Formula

For a connected control-flow graph with E edges, N nodes, and P connected components. In practice, mehen counts decisions plus 1:
where decisions are language-specific. Each language analyzer contributes increments for if, else if, loops, switch arms, && / ||, ternary expressions, exception handlers, and other control-flow constructs.

Per-language increments

The exact set of node kinds that increment complexity is owned by each language analyzer crate. The list matches McCabe’s original prescription closely:
  • Branches: if, else if, case/when arms.
  • Loops: for, while, do, loop, until.
  • Boolean operators: &&, || (each occurrence).
  • Ternary / conditional expression: the ?: operator.
  • Exception handlers: catch, rescue, except.
  • Early returns: counted in NEXITS, not cyclomatic.

How to read it

McCabe’s original 1976 paper recommended 10 as the upper limit per function, with rare exceptions for state machines.
Cyclomatic complexity counts paths, not understanding. A long chain of else if arms might score 8 but read clearly. A nested ternary scoring 4 might be near-impossible to follow. Pair this metric with Cognitive complexity.

References

  • McCabe, T. J. (1976). A Complexity Measure. IEEE Transactions on Software Engineering, SE-2(4): 308–320. DOI · PDF (literateprogramming.com archive).
  • Kearney, J. K., et al. Software Complexity Measurement — MIT lecture notes covering McCabe and Halstead together. PDF (MIT OCW 16.355).
  • Watson, A. H. & McCabe, T. J. (1996). Structured Testing: A Testing Methodology Using the Cyclomatic Complexity Metric. NIST Special Publication 500-235. NIST PDF.
  • Sonar: Cyclomatic complexity.

See also