Skip to content
Cyclomatic Complexity
step 1/5

Reading — step 1 of 5

Read

~1 min readCommon Rules

Cyclomatic Complexity

A function's cyclomatic complexity = number of independent paths through it. Roughly:

complexity = 1 + (count of branches: if, elif, while, for, except, and, or, ternary, case)

Why measure?

  • Higher complexity → harder to test (more paths to cover).
  • Empirically, complexity > 10 correlates with bugs.

Common thresholds:

  • ≤ 10: green.
  • 11-20: yellow (review).
  • 20: red (refactor).

python
python

Other complexity metrics:

  • Cognitive complexity: similar but penalizes nesting more.
  • Halstead metrics: ops + operands.
  • NPath: counts unique acyclic paths (can explode).

Linter typically reports cyclomatic; humans read the function.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…