# Dead code and unused exports eliminated

- **Pattern:** `ab-002160` (`code-quality-essentials.organization.dead-code-removed`)
- **Severity:** low
- **Lifecycle:** active
- **Last modified:** 2026-04-18
- **Canonical URL:** https://auditbuffet.com/patterns/ab-002160
- **License:** CC-BY-4.0 — attribute to AuditBuffet Pattern Catalog (https://auditbuffet.com/patterns/ab-002160)

## Why it matters

Commented-out code blocks and abandoned files (`api-old.ts`, `utils.backup.ts`) create ambiguity about which implementation is authoritative and inflate the mental model required to understand the codebase. ISO 25010:2011 §6.5.2 classifies this as a maintainability defect. For AI-built projects, the risk is compounded: a new coding session may incorrectly treat the abandoned code as the canonical version and reintroduce bugs that were already fixed in the active implementation.

## Severity rationale

Low because dead code does not cause runtime failures but creates maintenance overhead and, in AI-assisted workflows, risks reintroducing previously fixed bugs from the abandoned version.

## Remediation

Delete dead code — version control history preserves it if you ever need it back. Enable TypeScript compiler flags to catch unused variables and parameters automatically:

```json
{
  "compilerOptions": {
    "noUnusedLocals": true,
    "noUnusedParameters": true
  }
}
```

For unused exports across the entire project, run:
```bash
npx ts-unused-exports tsconfig.json
```

For commented-out code blocks, configure an ESLint rule or use a pre-commit hook that flags multi-line comment blocks containing what looks like executable code.

## Detection

- **ID:** `dead-code-removed`
- **Severity:** `low`
- **What to look for:** Look for large blocks of commented-out code in source files — this is a reliable signal that code was "removed" without being deleted, often because the developer wasn't sure if it would be needed again. Also look for: unused variables or imports that TypeScript would flag (`noUnusedLocals`, `noUnusedParameters` in tsconfig), exported functions or components that appear never to be imported anywhere in the codebase, entire files that appear to be old implementations retained alongside new ones (e.g., `api-old.ts`, `utils.backup.ts`, `component.v2.tsx`). Dead code increases maintenance burden and creates confusion about which implementation is authoritative.
- **Pass criteria:** Enumerate all relevant code locations. No significant commented-out code blocks, no obviously unused exports, and no clearly abandoned files alongside active replacements with at least 1 verified location.
- **Fail criteria:** Multiple large blocks of commented-out code, or clearly obsolete files retained alongside current implementations.
- **Skip (N/A) when:** Never — dead code applies to all projects.
- **Detail on fail:** `"Found large commented-out code blocks in src/components/OldModal.tsx and src/lib/api-v1.ts; dead code should be removed"`
- **Remediation:** Delete dead code — version control history preserves it if needed. Enable TypeScript flags to catch unused code automatically:

  ```json
  {
    "compilerOptions": {
      "noUnusedLocals": true,
      "noUnusedParameters": true
    }
  }
  ```

  For unused exports across the project, run:
  ```bash
  npx ts-unused-exports tsconfig.json
  ```

  Remove any flagged exports that are not part of a public API.

## External references

- iso-25010 maintainability.analysability

Taxons: code-quality

HTML version: https://auditbuffet.com/patterns/ab-002160
