cognitive-complexity

ProComplexity

Enforce a maximum cognitive complexity per function to catch AI-generated tangled logic

cognitive-complexity

Enforce a maximum cognitive complexity per function to catch AI-generated tangled logic

Category: Complexity | Tier: Pro

Why This Matters

AI-generated code often has deeply nested conditional logic that is difficult for humans to follow. High cognitive complexity means more mental effort to understand, review, and modify the code safely.

Bad Code

// Deeply nested, hard-to-follow logic
function process(data) {
  if (data) {
    for (const item of data) {
      if (item.active) {
        if (item.type === 'A') {
          // ... deeply nested
        }
      }
    }
  }
}

Good Code

// Flat, early-return style
function process(data) {
  if (!data) return;
  for (const item of data) {
    if (!item.active) continue;
    processItem(item);
  }
}

Configuration

This rule wraps sonarjs/cognitive-complexity. See upstream documentation for full configuration options.