Formula

CC = 1 + number of decision points

Where decision points are: if, while, do…while, for, for…in, for…of, switch case, catch, ternary expression, and binary && / || / ??.

What it measures

Cyclomatic Complexity (CC), introduced by Thomas McCabe in 1976, quantifies the structural complexity of a function by counting its independent execution paths. A function with no branching has CC = 1. Every additional decision point adds one more path.

Higher CC correlates with increased testing effort (you need at least as many test cases as CC to achieve full branch coverage) and higher likelihood of bugs.

How it's calculated in tsmetrics

tsmetrics walks the tree-sitter AST of the function body and counts these node kinds:

Nested functions are not counted in the enclosing function — each function is its own unit.

TypeScript example

TypeScript
function classify(score: number): string {  // CC = 1
  if (score >= 90) {                            // +1 → CC = 2
    return "A";
  } else if (score >= 80) {                    // +1 → CC = 3
    return "B";
  } else if (score >= 70) {                    // +1 → CC = 4
    return "C";
  }
  return score >= 60 ? "D" : "F";              // +1 (ternary) → CC = 5
}

Expected output

File Function Line LOC SLOC Complexity Nesting Params example.ts classify 1 9 8 5 2 1

Thresholds

SeverityConditionDefault value
OKCC < 10
WarningCC ≥ 1010
ErrorCC ≥ 2525

Configuration

thresholds: cyclomatic_complexity: warning: 10 # default error: 25 # default

References

Related metrics

Halstead Volume · Maintainability Index · WMC · Brain Method

Edit on GitHub