Cyclomatic Complexity
What is Cyclomatic Complexity?
Cyclomatic complexity is a software metric that measures the number of independent linear paths through a program's source code, used to gauge the complexity of a piece of code.
How It's Calculated
Using a Control Flow Graph (CFG), cyclomatic complexity is calculated as:
V(G) = E − N + 2, where E is the number of edges and N is the number of nodes in the graph.
Why It Matters for Testing
- It indicates the minimum number of test cases needed to achieve full path coverage.
- Higher complexity values suggest code that is harder to test, understand, and maintain.
- It helps identify modules that may need to be refactored to reduce risk.
Three Ways to Calculate the Same Number
The edges-and-nodes formula is the classic one, but there are two quicker methods that give the same result for well-structured code.
- Graph formula: V(G) = E − N + 2P, where P is the number of connected components. For a single function, P = 1, which gives the familiar E − N + 2.
- Decision count: V(G) = number of decision points + 1. Each
if,while,for,caseand ternary adds one. - Regions: draw the control flow graph flat and count the enclosed areas plus the outside area.
Worked Example: A Loop With a Condition Inside
This Java method adds up the positive numbers in an array.
int sumPositive(int[] values) {
int sum = 0; // node 1
for (int v : values) { // node 2 (loop check)
if (v > 0) { // node 3
sum += v; // node 4
}
}
return sum; // node 5
}
The control flow graph has five nodes and these six edges: 1→2, 2→3 (more items), 2→5 (loop finished), 3→4 (positive), 3→2 (not positive), 4→2. So V(G) = 6 − 5 + 2 = 3. The decision method agrees: two decisions (the loop and the if) plus one equals 3.
The three independent paths, and a test for each, are:
- 1 → 2 → 5: an empty array, expected sum 0.
- 1 → 2 → 3 → 2 → 5: the array
{-4}, expected sum 0. - 1 → 2 → 3 → 4 → 2 → 5: the array
{7}, expected sum 7.
Compound conditions
A line such as if (age >= 18 && hasId) hides two decisions. Many tools count each && and || as an extra point, because short-circuit evaluation creates an extra branch. Be consistent about which rule you use when comparing modules.
Reading the Score and Measuring It Automatically
Thomas McCabe, who introduced the metric, suggested keeping functions at 10 or below. Teams often adopt their own limits, but the idea holds: the higher the number, the more tests a function needs and the harder it is to understand.
# Python: report complexity per function with radon
pip install radon
radon cc my_module.py -s
# JavaScript: fail linting when a function gets too complex (.eslintrc)
"rules": { "complexity": ["error", 10] }
SonarQube, PMD and many IDE plugins show the same metric for Java, C# and other languages, often right beside the method name.
Lowering Complexity and Avoiding Misuse
- Extract parts of a long method into small, well-named helper methods.
- Replace long if-else chains with a lookup table or polymorphism.
- Use early returns for invalid input instead of deep nesting.
- Do not treat the number as a quality grade on its own. A simple switch over many cases scores high yet may be easy to read.
- Remember that complexity gives the size of a basis set of paths, not every possible path.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.