agentleFS
Cody-SimsCody-Sims/agent-skills/skills/refactoring-and-dead-code-removal/SKILL.md

refactoring-and-dead-code-removal

Skill0 starsChanged 4 months agoMITView on GitHub

Changes code structure without changing behavior and removes unused code safely by establishing a passing baseline, making one mechanical change at a time, re-running tests between steps, and proving code is dead before deleting it. Use when cleaning up, simplifying, restructuring, extracting, renaming, or deleting suspected unused code, imports, functions, or files.

The file

We split this skill into the rules it gives an agent

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

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈9

    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:

    Ensure behavior is covered.

    line 29
  2. Distinctive:

    Keep refactoring and behavior change in separate commits.

    line 37
  3. Distinctive:

    Do not remove or change code until you understand why it exists.

    line 43
  4. Distinctive:

    If you cannot explain its purpose, assume it has one and investigate further rather than deleting it.

    line 48
  5. Distinctive:

    Only remove it once you can state what it did and why removing it is safe.

    line 50
  6. Distinctive:

    Search for every reference to the symbol across the whole repository, including tests, configuration, string-based lookups, and dynamic …

    line 57
  7. Distinctive:

    Check the call graph: confirm nothing reachable calls it, directly or through an interface, export, or reflection.

    line 59
  8. Distinctive:

    Remove it, then run the full test suite and build to confirm nothing breaks.

    line 62
  9. Distinctive:

    If removal is large or risky, deprecate first, then remove after confirming no use.

    line 63

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 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

Refactoring and Dead-Code Removal

Goal

Improve structure or remove unused code while preserving observable behavior exactly, with test evidence at every step.

Core rule

Refactoring never changes behavior. If a change alters what the program does, it is a behavior change and belongs in a separate commit with its own tests. Keep the two apart so a regression is easy to locate and revert.

Workflow

  1. Establish a passing baseline. Run the repository's test command and build before touching anything. If the baseline is not green, stop and fix or record that first; you cannot detect regressions against a broken baseline.
  2. Ensure behavior is covered. If the code you will change lacks tests that pin its current behavior, add characterization tests first so a regression will surface.
  3. Make one mechanical change at a time. Rename, extract, inline, or move a single thing. Prefer the language or IDE's automated refactoring tools, which preserve behavior more reliably than manual edits.
  4. Re-run tests between steps. After each change, run the affected tests. A failure isolates the single step that caused it.
  5. Keep refactoring and behavior change in separate commits. Never mix them.
  6. Review the diff. Confirm it only restructures and does not alter logic, defaults, or outputs.

Chesterton's Fence

Do not remove or change code until you understand why it exists. Code that looks pointless often handles a case that is not obvious from a first reading.

  1. Find out why the code is there: read history, related tests, comments, and linked issues.
  2. If you cannot explain its purpose, assume it has one and investigate further rather than deleting it.
  3. Only remove it once you can state what it did and why removing it is safe.

Proving code is dead

Suspected-unused is not proven-unused. Gather evidence before deleting. See references/dead-code-evidence.md.

  1. Search for every reference to the symbol across the whole repository, including tests, configuration, string-based lookups, and dynamic access.
  2. Check the call graph: confirm nothing reachable calls it, directly or through an interface, export, or reflection.
  3. Confirm it is not part of a public API that external consumers depend on.
  4. Remove it, then run the full test suite and build to confirm nothing breaks.
  5. If removal is large or risky, deprecate first, then remove after confirming no use.

Forbidden approaches

  • Mixing a behavior change into a refactoring commit.
  • Deleting code because it looks unused without reference and call-graph evidence.
  • Refactoring against a red or unknown baseline.
  • Large sweeping rewrites in a single step that cannot be verified incrementally.

Output

Report the baseline result, each mechanical step taken, the evidence gathered before any deletion, and the passing test and build output after the changes.

References

  • references/dead-code-evidence.md — gathering reference, call-graph, and test evidence before removing code, and handling dynamic access and public APIs.

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.