Skip to content

Complexity ​

Cyclomatic Complexity ​

complexity.cyclomatic — Number of linearly independent paths (McCabe).

ItemValue
Definition1 + number of decision points in a function.
Scopefunction, file, project
InputNode kinds: branch, loop, case, catch, logical, conditional
CalculationFunction: 1 + decision nodes, excluding nested functions. Each else if / elif, each short-circuit operator (&&, ||, and, or), each ternary and each non-default case label is one decision. File: sum over its functions + decisions in top-level code. Project: sum over files.
Unitcount
Language Applicabilitypartially_language_dependent
LimitationsWhich constructs are decisions follows each language Mapping (e.g. Python comprehension for/if clauses count; Python case _: and Rust _ => count as cases).
ReferenceMcCabe, T. J. (1976). A Complexity Measure. IEEE TSE SE-2(4).

Branch Count ​

complexity.branch_count — Number of branch nodes (if, else if, elif) plus non-default case labels.

ItemValue
DefinitionNumber of branch nodes (if, else if, elif) plus non-default case labels.
Scopefunction, file, project
InputNode kinds
CalculationFunction: excluding nested functions. File: the whole file. Project: sum over files.
Unitcount
Language Applicabilitypartially_language_dependent
LimitationsWhich constructs map to each node kind follows each language Mapping.
Reference-

Conditional Count ​

complexity.conditional_count — Number of conditional (ternary) expressions.

ItemValue
DefinitionNumber of conditional (ternary) expressions.
Scopefunction, file, project
InputNode kinds
CalculationFunction: excluding nested functions. File: the whole file. Project: sum over files.
Unitcount
Language Applicabilitypartially_language_dependent
LimitationsWhich constructs map to each node kind follows each language Mapping.
Reference-

Loop Count ​

complexity.loop_count — Number of loops.

ItemValue
DefinitionNumber of loops.
Scopefunction, file, project
InputNode kinds
CalculationFunction: excluding nested functions. File: the whole file. Project: sum over files.
Unitcount
Language Applicabilitypartially_language_dependent
LimitationsWhich constructs map to each node kind follows each language Mapping.
Reference-

Return Count ​

complexity.return_count — Number of return statements.

ItemValue
DefinitionNumber of return statements.
Scopefunction, file, project
InputNode kinds
CalculationFunction: excluding nested functions. File: the whole file. Project: sum over files.
Unitcount
Language Applicabilitypartially_language_dependent
LimitationsWhich constructs map to each node kind follows each language Mapping.
Reference-

Jump Count ​

complexity.jump_count — Number of jumps: break, continue, goto and throw / raise.

ItemValue
DefinitionNumber of jumps: break, continue, goto and throw / raise.
Scopefunction, file, project
InputNode kinds
CalculationFunction: excluding nested functions. File: the whole file. Project: sum over files.
Unitcount
Language Applicabilitypartially_language_dependent
LimitationsWhich constructs map to each node kind follows each language Mapping.
Reference-

Number of Paths ​

complexity.path_count — Acyclic execution paths through a function.

ItemValue
DefinitionNumber of paths through the function when each loop runs zero times or once.
Scopefunction
InputNode kinds and tree structure
CalculationChildren in sequence multiply. An if-chain is the sum of its arms, +1 without a final else. A loop or ternary is its children's product + 1. Consecutive case labels or catch clauses are the sum of their paths + 1. Nested functions count as 1.
Unitcount
Language Applicabilitypartially_language_dependent
LimitationsNot Nejmeh's NPATH: short-circuit operators and early exits (return, jump) do not change the count.
ReferenceNejmeh, B. A. (1988). NPATH: a measure of execution path complexity. CACM 31(2) (related, not identical).

Cognitive Complexity ​

complexity.cognitive — How hard a function's control flow is to understand (SonarSource).

ItemValue
DefinitionSum of increments for breaks in linear flow, weighted by nesting.
Scopefunction, file, project
InputNode kinds, parent links, call and logical labels
Calculationif-chain head, loop, catch, ternary, and each run of case labels (a switch): 1 + nesting level. else if / elif and else: 1. Each sequence of like logical operators: 1. A call to the function's own name (recursion): 1. Nesting levels are opened by branches, loops, cases, catches and ternaries. File: sum over functions. Project: sum over files.
Unitcount
Language Applicabilitypartially_language_dependent
LimitationsNested functions (lambdas) are measured separately instead of adding to the enclosing function. Labelled break / continue and goto add nothing (jumps have no labels in the IR).
ReferenceCampbell, G. A. (2018). Cognitive Complexity: A new way of measuring understandability. SonarSource.