Cyclomatic Complexity

Updated Sep 18, 2026

Cyclomatic complexity is a software metric based on the number of independent paths through a function, method, or module. Branches such as conditions and loops increase it. In a legacy codebase audit, the metric helps locate logic that may require more tests and closer review before it is changed.

Cyclomatic complexity describes the number of independent execution paths through a unit of code. Each decision point adds another possible route. The metric gives a legacy codebase audit a consistent way to locate functions or modules whose control flow may be difficult to understand, verify, and change.

What the metric counts

Straight-line code has one route from entry to exit. Conditional branches, loops, and other control-flow decisions add paths. Analysis tools calculate the metric from a control-flow graph, so the exact treatment of language constructs can vary between tools.

The number is a structural signal. It does not judge whether the business rule is necessary or whether the implementation is correct. A short function with many conditions can score higher than a long function that performs one linear sequence.

Code shapeComplexity signalReview question
Straight sequenceFew independent pathsIs the behaviour clear and covered?
Nested conditionsSeveral possible routesCan each outcome be verified?
Repeated branchingLogic spread across many casesAre responsibilities mixed?
Exception and fallback pathsAlternative failure routesDo tests cover degraded behaviour?

Why it matters in legacy code

Complex control flow makes change harder because a developer must preserve behaviour across more paths. In an older system, some of those paths may represent business exceptions that are no longer documented. Others may be defensive code for integrations or data formats that still exist in production.

During a legacy codebase audit, high-complexity areas receive closer reading. The metric helps locate them, but maintainers, tests, and runtime evidence explain why the branches exist.

How an audit uses the result

The metric is most useful as a filter across a large repository. An audit can compare it with change history and test evidence to find code that is complex, frequently modified, and poorly protected. That combination is more actionable than any one measure.

The review then asks:

  • Which paths matter? Identify normal, exceptional, and fallback behaviour.
  • Which paths are tested? Match tests to outcomes rather than lines alone.
  • Why do branches exist? Separate current rules from obsolete workarounds.
  • Can logic be divided? Isolate responsibilities without changing behaviour.

What the metric cannot decide

Cyclomatic complexity does not measure architecture, readability, security, or business value. It can also understate difficulty when simple functions are tightly connected across a system. A low score therefore does not prove that code is safe to change.

An audit pairs the metric with technical debt, dependency analysis, and direct review. This broader evidence helps a team decide where refactoring reduces real change risk. It also helps scope legacy code migration work around behaviour that must be understood before it moves.