What Is Cyclomatic Complexity?
Cyclomatic complexity counts the independent paths through a program's control-flow graph. It is computed as M = E − N + 2P, where E is the number of edges, N the number of nodes, and P the number of connected components.
A higher value means more decision points — ifs, loops, and cases — and therefore more test cases to exercise every path. The metric is a widely used guide to code that may be hard to test and maintain.
Formula
Applications
- Estimating how many test cases are needed to cover a function
- Flagging functions that are too complex to maintain comfortably
- Guiding code refactoring when complexity climbs above a threshold
- Comparing the relative complexity of modules in a codebase
- Enforcing quality gates in automated code review tools
Sources
- McCabe, T. J. (1976). A Complexity Measure. IEEE Transactions on Software Engineering.
- Pressman, R. S. (2014). Software Engineering: A Practitioner’s Approach (8th ed.). McGraw-Hill.