A complexity budget for AI-written code

How cyclomatic complexity can help review AI-generated code without mistaking one number for code quality

Code can compile, pass its tests, and still be hard to change. The difficulty often appears later, when a small feature means tracing conditions, loops, and early exits through a function no one quite understands.

AI tools make producing code faster. That is useful, but it also makes it easier to add branches faster than a reviewer can reason about them. Cyclomatic complexity offers one modest way to notice that risk. It is not a verdict on whether code is good, and AI-generated code is not automatically more complex. The point is to make a review question visible: how many distinct routes through this function should someone understand and test?

Count independent routes through the code

Thomas McCabe introduced cyclomatic complexity in 1976 as a measure based on a program's control-flow graph. In practical terms, it counts linearly independent paths through a function: paths that add a new route through its decisions rather than simply repeating a combination already represented by other paths.

For a control-flow graph, the formula is M = E − N + 2P: E is the number of edges, N the number of nodes, and P the number of connected components. For one function, P is usually one. In ordinary structured code, a handy approximation is one plus the number of decision points. Exact counts can differ by language and analyzer, especially around compound boolean expressions, so use the definition and configuration of the tool you adopt—not an assumed universal counter.

For example:

function discountFor(order) {
  let discount = 0;

  if (order.isMember) {
    discount = 5;
  }

  if (order.total > 100) {
    discount += 10;
  }

  return discount;
}

There are two decisions, so the function's cyclomatic complexity is 3. There are four possible combinations of the two conditions: neither, either one, or both. A basis-path test set needs three linearly independent routes; testing every combination takes four tests. That distinction matters: the score is not a count of every possible execution, nor a promise that three tests cover every behavior or edge case. In a real control-flow graph, some routes may also be impossible for any valid input.

Why it can help with AI-assisted changes

A generated patch can look tidy line by line while accumulating nested conditions and exceptions. A complexity report does not tell me whether those branches are justified, but it can point me to the functions where a careful read and focused tests are most valuable.

I would use it as a review prompt, not as an automatic demand to split every function. A score says something about control flow, not naming, cohesion, data flow, error handling, or whether a business rule is correct. Two functions with the same score can be very different to understand. Some studies report an association between complexity and defects, but that does not prove that lowering a score prevents bugs. Complexity also tends to rise with program size, which makes the relationship difficult to interpret; defect-prediction research cautions against treating one metric as a reliable standalone forecast.

The number is local to the code being measured. Extracting a branch-heavy block into a helper can lower the original function's score without removing complexity from the system. That can still be a good refactor if the helper has a clear responsibility and makes behavior easier to test. Splitting code solely to satisfy a threshold can just move the problem—or make it harder to follow.

Introduce a budget without turning it into a game

McCabe recommended keeping a module's complexity at 10 or below as a development guideline. That is a useful historical reference, not a universal law for every language, codebase, or function. Before setting a limit, I would check what the chosen analyzer counts and look at the existing distribution. A threshold that flags half the repository on day one is more likely to create noise than better reviews.

My rollout would be deliberately incremental:

  1. Measure functions consistently. Choose a maintained, language-aware analyzer, pin its relevant configuration, and verify a few sample functions by hand. Record the tool's counting rules.
  2. Start with visibility. For example, a JavaScript or TypeScript project could configure ESLint's complexity rule at a warning threshold of 10, then publish findings in pull requests without blocking them. Check the rule's options and sample output; the threshold is a starting point, not a universal target.
  3. Focus on new or changed code. Keep existing findings as a baseline instead of requiring unrelated rewrites whenever someone edits a file. Once the team has calibrated the signal, use a baseline or diff-aware check to fail CI only for newly introduced violations.
  4. Ask for an explanation, not just a lower number. Above the agreed limit, consider simplifying the flow, extracting a cohesive responsibility, or documenting why the shape is clearer as-is. Review whether complexity was removed or merely relocated, and require review for exceptions.
  5. Pair the metric with tests. Use the score to think about independent routes and missing cases. Then test the important behavior, including boundary conditions and error paths; do not treat the score as a coverage certificate.

For AI-assisted work, I can also ask the tool to list the decisions it introduced, explain the meaningful routes, and suggest tests before it edits. I still need to inspect that explanation against the code and requirements. Generated tests are useful proposals, not independent evidence that the implementation is right.

Keep the number in its place

Cyclomatic complexity is valuable because it is cheap to compute and gives a team a shared signal about branching and test effort. It is limited because code quality is bigger than control flow. A low score cannot guarantee readable or correct code; a high one does not automatically mean the code should be split.

In an AI-assisted codebase, I would use the metric to direct human attention toward concentrated decision-making, especially in changed code. The budget is there to start a conversation about responsibility, paths, and tests—not to optimize code for a dashboard.

Sources

Have a question or a different take?