Cyclomatic Complexity

ID

python.cyclomatic_complexity

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Complexity

Language

Python

Tags

complexity

Description

Reports Python functions whose cyclomatic complexity (CCN) exceeds a configurable threshold. CCN counts the linearly independent paths through a function — every if / elif / for / while / try / and / or adds one. High CCN correlates with hard-to-test, hard-to-maintain code; the value is the canonical single-number summary of branching depth.

Rationale

Functions above CCN ~30 are routinely the source of "I touched one branch, broke another I didn’t realise existed" defects. Splitting them along the natural axes (per-case helpers, dispatch tables, early returns) usually produces cleaner code with the same total complexity but no single function carrying it all.

Configuration

Property Default Meaning

maxCcn

30

Functions with CCN strictly greater than this value are flagged.

Tighten the threshold (e.g. maxCcn: 20) to match the team’s preferred ceiling.

Remediation

Refactor the function:

  • extract helpers for the inner branches,

  • replace long if/elif/elif chains with a dispatch table or polymorphism,

  • prefer early returns over deep nesting.