# 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]

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](https://wiki.g15e.com/en/pages/Guard%20clauses.txt) at the beginning of the function does not affect CC. This is because CC, unlike [Cognitive complexity](https://wiki.g15e.com/en/pages/Cognitive%20complexity.txt), does not assign weight to nested branches. (Reference: [A case for early returns](https://wiki.g15e.com/en/pages/A%20case%20for%20early%20returns.txt))

## Alternatives

Cyclomatic complexity is good for measuring testability, but it is insufficient for measuring maintainability, so an indicator called [cognitive complexity](https://wiki.g15e.com/en/pages/Cognitive%20complexity.txt) has been proposed to supplement this.

## Footnotes

[^1]: [A complexity measure](https://wiki.g15e.com/en/pages/A%20complexity%20measure.txt)