liveBook · Manning

Cyclomatic complexity

As explained in AI-Powered Developer: Build software with ChatGPT and Copilot

Cyclomatic complexity is a metric that counts the independent execution paths through a software module based on its decision points.

Cyclomatic complexity counts the independent execution paths through a software module. It reflects decision-making created by loops, conditionals, and branches: higher values generally indicate code that is harder to understand, maintain, and test, with greater potential for bugs. section 6.2 section 6.7

Use the metric to guide practical code review:

  • Prefer lower complexity when possible because simpler code is easier to reason about and maintain. section 6.2
  • Treat higher complexity as a signal to examine logic carefully and anticipate a larger testing burden. section 6.2
  • Interpret the metric alongside code coverage, Halstead measures, and the maintainability index rather than as a complete quality judgment. section 6.7

Querying a class with Copilot

Ask Copilot, “What is the cyclomatic complexity of the class Department-StatisticsVisitor?” The demonstrated answer is 1. A follow-up question—“Why is 1 a good value?”—returns “Because it is low,” illustrating the metric’s basic interpretation without replacing review or testing. section 6.2

# Question: What is the cyclomatic complexity of the class Department- StatisticsVisitor?
# Answer: 1

Inspect the compact question-and-answer example for a direct complexity assessment. section 6.2

How AI-Powered Developer: Build software with ChatGPT and Copilot covers it

How deep it goes
Defines independent paths and connects higher values to maintenance and testing difficulty, but does not derive a calculation formula.
Examples
Uses Copilot questions about the Department-StatisticsVisitor class and an observed complexity value of 1.
What it assumes
Assumes familiarity with software modules and decision-making constructs while defining the metric from those constructs.
What you can do after
Evaluate code structure with a complexity reading and identify when additional testing or simplification may be warranted.
How it connects
Connects loops, conditionals, branches, testing, code coverage, Halstead measures, and the maintainability index.

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 AI-Powered Developer: Build software with ChatGPT and Copilot. The section links point to the parts of the book it draws on.