Refactor clean

From drvoss/everything-copilot-cli · View interactive version

Refactor clean is an AI agent skill from the GitHub repository drvoss/everything-copilot-cli. Use when code has grown complex, duplicated, or cluttered — clean up structure and remove dead code without changing observable behavior

How to use this skill

Skill file

Refactor & Clean Code

When to Use

Prerequisites

Workflow

1. Identify Code Smells

# Find long files (likely doing too much)
Get-ChildItem -Recurse -Include *.ts,*.js,*.py,*.go | Where-Object { (Get-Content $_.FullName | Measure-Object -Line).Lines -gt 300 } | Select-Object FullName, @{N='Lines';E={(Get-Content $_.FullName | Measure-Object -Line).Lines}}

# Find duplicated string patterns
grep -rn "pattern-you-suspect-is-duplicated" src/ --include="*.ts"

# Find functions with too many parameters (>4 is a smell)
grep -n "function.*,.*,.*,.*," src/**/*.ts

Common smells to look for:

2. Establish a Safety Net

# Run existing tests and record baseline
npm test 2>&1 | Tee-Object -Variable baseline
echo $baseline | Select-Object -Last 5

# If coverage is low, write characterization tests first
npm test -- --coverage --collectCoverageFrom="src/module-to-refactor.ts"

3. Plan the Refactoring

Before touching code, decide on the transformation:

4. Make Small, Incremental Changes

Each change should be:

  1. A single refactoring operation
  2. Followed by running tests
  3. Committed if tests pass
# After each refactoring step
npm test 2>&1 | Select-Object -Last 10

# If tests break, revert and try a smaller step
git checkout -- src/module-to-refactor.ts

5. Verify Behavior is Preserved

# Full test suite must still pass
npm test

# Compare coverage — it should not decrease
npm test -- --coverage

6. Slim a Fresh Diff by Ablation (optional)

When the code to clean is the current PR or working-tree change rather than old code, cut it down

to the smallest correct diff (concept adapted from antfu/skills antfu-simplify):

  1. Write the change's goal in one sentence. Every remaining line must be explained by it.
  2. List each piece the diff adds — helper, parameter, branch, type, file, dependency, comment, test.
  3. For each piece, try deleting, inlining, or hardcoding it and run the focused checks. Keep it if

something that matters breaks. If no check covers it, keep it unless you can show the case it

guards is unreachable.

  1. Revert drive-by edits that the goal does not explain. Do not change behavior or public APIs.
  2. Report what was cut or kept and why, with git diff --stat before and after.

Cut speculative options, pass-through wrappers, one-use helpers, and guards against impossible

input. Keep handling for real edge cases and the general fix over a special case.

Examples

Extract Repeated Logic

// BEFORE: duplicated validation in multiple handlers
function createUser(data) {
  if (!data.email || !data.email.includes('@')) throw new Error('Invalid email');
  // ... create logic
}
function updateUser(data) {
  if (!data.email || !data.email.includes('@')) throw new Error('Invalid email');
  // ... update logic
}

// AFTER: extracted to a shared validator
function validateEmail(email: string): void {
  if (!email || !email.includes('@')) throw new Error('Invalid email');
}
function createUser(data) { validateEmail(data.email); /* ... */ }
function updateUser(data) { validateEmail(data.email); /* ... */ }

Flatten Deep Nesting with Early Returns

// BEFORE
function process(input) {
  if (input) {
    if (input.isValid) {
      if (input.data.length > 0) {
        return transform(input.data);
      }
    }
  }
  return null;
}

// AFTER
function process(input) {
  if (!input || !input.isValid || input.data.length === 0) return null;
  return transform(input.data);
}

Common Rationalizations

| Rationalization | Reality |

|----------------|---------|

| "I'm not sure what this code does, but it looks unused" | If you're not sure, don't remove it. Use git blame to understand the context first. (Chesterton's Fence) |

| "I'll add a feature while refactoring" | Separate refactoring from feature work. Mixing the two makes debugging impossible. |

| "Tests still pass, so the refactor is correct" | Tests must exist before refactoring to have any meaning. If not, write them first. |

| "TypeScript isn't warning, so it's safe to delete" | Dynamic imports, reflection, and external references are invisible to the type system. |

Red Flags

Verification

Tips

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.