Advanced Code Quality #cyclomatic-complexity #cognitive-complexity #maintainability-index

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?