Code Complexity Metrics Vocabulary
5 exercises — master the vocabulary of code complexity: cyclomatic and cognitive complexity, maintainability index, Halstead metrics, and using complexity as a risk proxy.
0 / 5 completed
1 / 5
A tech lead reviews a pull request and opens SonarQube, pointing at a result: "This function has a cyclomatic complexity of 15." What does cyclomatic complexity measure, and what does a score of 15 signify?
Cyclomatic complexity is the most widely adopted code complexity metric in the industry — understanding it precisely is essential for code review and quality discussions.
How it is calculated (McCabe, 1976):
M = E − N + 2P
where E = edges (control-flow transitions), N = nodes (code blocks), P = connected components (usually 1 per function).
In practice: M = number of decision points (if, else if, for, while, switch case, catch, &&, ||, ternary) + 1
Score of 15 — what it means operationally:
• Requires a minimum of 15 unit test cases to achieve full branch coverage
• Statistically correlated with higher defect density (Basili et al., NASA research)
• SonarQube flags complexity >10 as a code smell requiring refactoring or justified exception
Key vocabulary:
• Cyclomatic complexity — the number of linearly independent paths through code
• Decision point — any branch in the control flow (if, for, while, case, &&, ||)
• Defect density — the ratio of known defects to a unit of code size or complexity
How it is calculated (McCabe, 1976):
M = E − N + 2P
where E = edges (control-flow transitions), N = nodes (code blocks), P = connected components (usually 1 per function).
In practice: M = number of decision points (if, else if, for, while, switch case, catch, &&, ||, ternary) + 1
| Score | Risk | Interpretation |
|---|---|---|
| 1–10 | Low | Simple, well-structured; easy to test |
| 11–20 | Medium | Moderately complex; coverage harder to achieve |
| 21–50 | High | Error-prone; refactoring recommended |
| >50 | Very High | Practically untestable; urgent refactoring needed |
Score of 15 — what it means operationally:
• Requires a minimum of 15 unit test cases to achieve full branch coverage
• Statistically correlated with higher defect density (Basili et al., NASA research)
• SonarQube flags complexity >10 as a code smell requiring refactoring or justified exception
Key vocabulary:
• Cyclomatic complexity — the number of linearly independent paths through code
• Decision point — any branch in the control flow (if, for, while, case, &&, ||)
• Defect density — the ratio of known defects to a unit of code size or complexity