liveBook · Manning

Cyclomatic complexity

As explained in The Art of Code

Cyclomatic complexity is a software metric that counts the independent execution paths through a program's control flow based on its decision points.

Cyclomatic complexity makes control-flow complexity visible by counting the independent execution paths through a method. Decision points such as if, while, and switch statements determine the count, giving teams an objective signal for code review and static analysis. section 3.1.2

Reading the metric

Consider a method containing several if statements, a while loop, and a switch. Each decision creates additional possible routes through the method, so the cyclomatic score rises with the number of independent paths. Two methods can receive the same score yet differ substantially in readability when one uses deep nesting, breaks, or convoluted Boolean logic. section 3.1.2

Use the metric as a structural signal, then interpret it alongside readability concerns:

  • Compare methods by control-flow structure rather than treating the score as a complete measure of understandability. section 3.1.2
  • Pair cyclomatic complexity with cognitive complexity when nesting and mixed logical operators increase cognitive load. section 3.1.2 section 3.3
  • Treat high scores as signals to investigate, while recognizing that essential domain complexity may need containment rather than elimination. section 3.3

How The Art of Code covers it

How deep it goes
Defines independent execution paths and decision-point counting, but stops short of capturing readability, nesting, or Boolean entanglement fully.
Examples
Uses if, while, and switch statements as decision-point examples and contrasts equal scores with different readability costs.
What it assumes
Assumes familiarity with methods and basic control-flow constructs such as conditions, loops, and switches.
What you can do after
Evaluate methods with a structural metric and investigate whether refactoring or complexity containment is appropriate.
How it connects
Connects control flow, decision points, cognitive complexity, static analysis, and SonarQube.

Also explained in

Compare how 5 books explain cyclomatic complexity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Code. The section links point to the parts of the book it draws on.