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
Code complexity vocabulary quick reference
- Cyclomatic complexity — number of linearly independent paths through code (M = E − N + 2P); recommended max: 10
- Cognitive complexity — how hard code is to read; penalises nesting depth more than cyclomatic does
- Maintainability index — composite 0–100 score: 85–100 green, 65–84 yellow, 0–64 red
- Halstead volume — information content of code based on operator/operand token counts
- Halstead difficulty — how error-prone the code is to write and understand correctly
- Hotspot — a file with both high complexity and high churn; highest bug density risk
- Defect density — ratio of defects to a unit of code size; increases with complexity
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