agentleFS
bordenetbordenet/superpowers-plus/skills/engineering/cognitive-complexity-refactoring/skill.md

cognitive-complexity-refactoring

Skill8 starsChanged 6 months agoMITView on GitHub

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

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 2 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    When Biome reports an error:

    line 41
  2. Distinctive:

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

    line 157

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
skill.md

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

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.