agentsclimarketplace

Cognitive complexity refactoring

Skill bordenet/superpowers-plus/skills/engineering/cognitive-complexity-refactoring

Use when Biome reports noExcessiveCognitiveComplexity, when functions have deeply nested conditionals (3+ levels), or when refactoring large functions.From its SKILL.md

Install
npx -y skills add bordenet/superpowers-plus --skill cognitive-complexity-refactoring

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • 8 stars8 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

SKILL.md

4.0 KB, as published. Nobody here has run it

cognitive-complexity-refactoring

When to Use

Invoke this skill when:

  • Biome reports: noExcessiveCognitiveComplexity
  • Function has 3+ levels of nesting
  • You're reviewing a function >30 lines
  • Code has multiple nested if/else chains
  • You need to simplify complex logic

CRITICAL RULE: NO biome-ignore

biome-ignore comments are BANNED in new code.

When Biome reports an error:

  1. REQUIRED: Refactor the code to fix the actual issue
  2. REQUIRED: Extract functions, simplify logic, reduce nesting
  3. BANNED: Adding // biome-ignore to suppress the warning
// ❌ NEVER DO THIS
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: complex
function handleMessage(data: Buffer): void {
  // 25 lines of nested if/else statements
}

// ✅ ALWAYS DO THIS - Extract to smaller functions
function handleMessage(data: Buffer): void {
  const message = parseMessage(data)
  if (message.type === 'partial') {
    handlePartialResult(message)
  } else if (message.type === 'final') {
    handleFinalResult(message)
  }
}

function handlePartialResult(message: Message): void {
  if (!message.text) return
  const event = createResultEvent(message, false)
  emitResult(event)
}

What is Cognitive Complexity?

Measures how hard a function is to understand.

Calculates based on:

  • Nesting depth (each level adds complexity)
  • Control flow structures (if, for, while, switch)
  • Logical operators (&&, ||)
  • Recursion

Threshold: Maximum 15 (configured in biome.json)


Refactoring Patterns

Pattern 1: Extract Nested Logic

// ✅ Complexity reduced by extraction
function processCoordinates() {
  for (let x = 0; x < 10; x++) {
    for (let y = 0; y < 10; y++) {
      processPoint(x, y)
    }
  }
}

function processPoint(x: number, y: number) {
  if (x % 2 !== 0) return
  if (y % 2 !== 0) return
  const max = x > y ? x : y
  console.log(max)
}

Pattern 2: Early Returns

// ✅ Complexity: 3
function validate(data: Data | null): boolean {
  if (!data) return false
  if (!data.isValid) return false
  if (!data.hasRequiredFields()) return false
  return true
}

// vs ❌ Complexity: 6
function validate(data: Data | null): boolean {
  if (data) {
    if (data.isValid) {
      if (data.hasRequiredFields()) {
        return true
      }
    }
  }
  return false
}

Pattern 3: Extract Conditions

// ✅ Reduce nesting
const canProcess = isActive && hasPermission && !isBlocked
if (!canProcess) return
processItem()

// vs ❌ Nested ifs
if (isActive) {
  if (hasPermission) {
    if (!isBlocked) {
      processItem()
    }
  }
}

File Size Limits

Standard: <300 lines per file Exception: Cohesive state machines (~350 lines acceptable)

If file >300 lines: Refactor into smaller focused files.


Verification

pnpm lint  # No Biome complexity warnings

A task is NOT complete until this passes.

Failure Modes

ModeSymptomRecovery
Over-refactorNew bugs introducedRun tests after each extraction
Wrong extraction boundaryMakes code harder to followKeep related logic together
Extracting too aggressivelyMany tiny functions obscure flowBalance complexity score with readability

Keep looking

Skills are one crate of 627,801. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.