Skip to content

Sonar-style cognitive complexity per function — branching breaks plus a nesting penalty. Deeply nested code costs more than a long flat switch. Tracks if/else if, loops, ternaries, switch, catch, sequences of &&/||/??, and recursion-by-name. Default limit is 15; override per-project with [checks."Refactor.CognitiveComplexity"] limit = N in cofferdam.toml.

ts
// flagged: nested branches stack a nesting penalty
function classify(record: Record) {
  if (record.kind === "user") {
    if (record.active) {
      for (const role of record.roles) {
        if (role.permissions.includes("admin")) {
          return "active-admin";
        }
      }
    }
  }
  return "other";
}
ts
// fix: flatten via early returns and helpers
function classify(record: Record) {
  if (record.kind !== "user" || !record.active) return "other";
  return hasAdmin(record.roles) ? "active-admin" : "other";
}

MIT License