Skip to main content

Core analyzer

Cyclomatic Complexity

Counts McCabe's decision points per function and grades a file by its most complex function.

technical-debt core-cyclomatic

Cyclomatic Complexity

Counts McCabe's decision points per function and grades a file by its most complex function.

McCabe's cyclomatic complexity (1976) is defined per function: 1, plus 1 for every if, else if, loop, case, catch, ternary, &&, ||, ??, ?. and logical assignment (&&=, ||=, ??=) in it. A function nested inside another one and bound to a name is measured on its own; an inline callback counts toward the function that contains it. Code outside every function is measured as one unit.

McCabe proposed 10 as the limit for one function, and that is what the limit is compared with: the most complex function unit of the file. Vipr classifies a unit up to 5 as simple, 6–10 as moderate, 11–20 as high, 21–30 as very high, and above 30 as critical.

The file total (1 plus every decision point in the file) is reported as well. It grows with every function a file contains, so it measures size: it is displayed, and it is never compared with the per-function limit.

Severity guide

info
The most complex function is moderate (6–10). Within McCabe's limit; worth checking whether some branches can be consolidated before they multiply.
warning
A function is above McCabe's limit of 10. It has more independent paths than tests usually cover, and it is harder to change safely.
critical
A function is above 30. No realistic test suite covers that many paths; split it before the next round of changes.

Examples

Before

function classify(user) {
  if (user.role === "admin" && user.active && !user.suspended) {
    if (user.permissions.includes("write")) return "editor";
    if (user.permissions.includes("read")) return "viewer";
    return "admin-no-perms";
  } else if (user.role === "guest") {
    return user.invited ? "guest" : "anonymous";
  }
  return "unknown";
}

After

function classify(user) {
  if (user.role === "guest") return classifyGuest(user);
  if (user.role !== "admin" || !user.active || user.suspended) return "unknown";
  return classifyAdmin(user);
}

function classifyAdmin(user) {
  if (user.permissions.includes("write")) return "editor";
  if (user.permissions.includes("read")) return "viewer";
  return "admin-no-perms";
}

function classifyGuest(user) {
  return user.invited ? "guest" : "anonymous";
}

Extracting cohesive branches into named helper functions reduces the cyclomatic complexity of each function. The same logic runs and the file contains the same decisions, but no single function has to be tested across the full Cartesian product of conditions — which is why the limit applies to a function and not to the file.

Remediation

Extract cohesive branches into helper functions and replace nested conditionals with early returns or strategy lookups.

  • Identify the largest branching cluster (often a multi-condition if/else ladder) and extract its body into a named helper.
  • Convert nested if chains into early-return guard clauses where the early branches handle the simple cases.
  • Replace large switch blocks or if/else if ladders with lookup objects (Map / Record) when the cases are data-driven.
  • Run the existing test suite after each extraction to confirm behavior is unchanged.

References

Documentation