Glossary/Cyclomatic Complexity
Technical Debt & Code Quality
2 min read
Share:

What is Cyclomatic Complexity?

TL;DR

Cyclomatic complexity is a quantitative measure of the number of linearly independent paths through a program's source code.

Cyclomatic complexity is a quantitative measure of the number of linearly independent paths through a program's source code. Invented by Thomas J. McCabe in 1976, it counts the number of decision points (if statements, loops, switch cases) plus one.

A function with no branches has complexity 1. Each if/else adds 1. Each loop adds 1. A function with complexity 10 has 10 independent paths that need to be tested for full coverage.

Benchmarks: 1-10 is simple and low risk. 11-20 is moderate complexity. 21-50 is high complexity and hard to test. Above 50 is untestable and should be refactored immediately.

High cyclomatic complexity is one of the strongest predictors of bugs. Research shows that modules with complexity >20 are 5x more likely to contain defects than modules with complexity <10. It's also the primary driver of long testing cycles — each independent path needs its own test case.

Why It Matters

Cyclomatic complexity is one of the most reliable leading indicators of maintenance cost and bug risk. It's measurable, actionable, and directly correlates with testing effort. Teams that enforce complexity limits (e.g., max 15 per function) consistently produce more maintainable, less buggy code.

How to Measure

1. **Per Function**: Use static analysis tools (SonarQube, ESLint, pylint) to measure complexity per function.

2. **Module Average**: Average complexity across all functions in a module.

3. **Hotspots**: Identify the top 10 most complex functions — these are your highest-risk code.

4. **Threshold**: Set a maximum (e.g., 15) and fail CI builds that exceed it.

Frequently Asked Questions

What is cyclomatic complexity?

Cyclomatic complexity counts the number of independent execution paths through a function. More paths = more complexity = harder to test and maintain.

What is a good cyclomatic complexity score?

1-10 is simple and low risk. 11-20 is moderate. Above 20 should be refactored. Above 50 is untestable.

Related Terms

Need Expert Help?

Richard Ewing is a Product Economist and AI Capital Auditor. He helps companies translate technical complexity into financial clarity.

Book Advisory Call →