Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mehen.ophi.dev/llms.txt

Use this file to discover all available pages before exploring further.

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

KeyTypeDescription
cyclomaticintTotal cyclomatic complexity for the space (function/method/file).
cyclomatic.sumintSum across child spaces.
cyclomatic.averagefloatMean across functions.
cyclomatic.minintMinimum across functions.
cyclomatic.maxintMaximum across functions.

Formula

CC = E − N + 2P
For a connected control-flow graph with E edges, N nodes, and P connected components. In practice, mehen counts decisions plus 1:
CC = 1 + decisions
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

ValueInterpretation
1–4Simple, low risk.
5–10Moderate, well within McCabe’s recommended ceiling.
11–20Complex; refactor candidate.
21+Untestable in practice — split into smaller functions.
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