Skip to content

Cyclomatic complexity

Cyclomatic complexity or CC (cyclomatic complexity) is an indicator that calculates the complexity of a program and quantifies how many linearly independent paths the code has. The lowest score is 1 (because if there are no branches, there is only 1 path). Thomas J. McCabe proposed it in 1976.1

If the cyclomatic complexity is low, test coverage (Branch coverage to be exact) can be increased even with a small number of unit tests.

Ways to lower CC

  • Conditionals to lookup table: CC can be reduced by using a lookup table or dictionary instead of branching. If the language supports it, it would be good to use pattern matching, but using pattern matching does not lower CC.
  • Conditionals to polymorphism: CC can be reduced by using Polymorphism instead of branching. However, doing this does not always guarantee improved code understandability or maintainability.

CC and early return

Contrary to common misunderstanding, using Guard clauses at the beginning of the function does not affect CC. This is because CC, unlike Cognitive complexity, does not assign weight to nested branches. (Reference: A case for early returns)

Alternatives

Cyclomatic complexity is good for measuring testability, but it is insufficient for measuring maintainability, so an indicator called cognitive complexity has been proposed to supplement this.

Footnotes

  1. A complexity measure

Created
Updated

Machine-translated from the Korean original.

2026 © ak | markdown | 한국어