agentleFS
SpencerGossSpencerGoss/agent-engineering/refactor-session/SKILL.md

refactor-session

Skill0 starsChanged 5 months agoMITView on GitHub

Use when improving code structure without changing behavior — cleaning up messy code, extracting functions, renaming for clarity, removing duplication. Trigger on: "refactor this", "clean up this code", "this is too messy", "extract this function", "this function is too long", "rename this", "remove duplication", or any structural improvement task. Hard rule: never refactor and change behavior in the same commit.

The file

We split this skill into the rules it gives an agent

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

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈10

    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:

    Hard rule: Never refactor and change behavior in the same commit.

    line 20
  2. Distinctive:

    Before jumping into structural changes:

    line 25
  3. Distinctive:

    Don't start moving code until you can describe where everything should end up.

    line 30
  4. Distinctive:

    Never batch multiple refactors.

    line 68
  5. Distinctive:

    NOT for fixing bugs or changing behavior — use a debug skill for bugs, a TDD skill for new behavior

    line 130
  6. Distinctive:

    NOT for adding new features while restructuring — use a spec-driven-development skill for feature work

    line 131
  7. Distinctive:

    NEVER use this for full rewrites or redesigns — if the refactor feels like a rewrite, scope it as a new feature instead

    line 132
  8. Distinctive:

    NOT for code review of someone else's work — use a code-review skill instead

    line 133
  9. Distinctive:

    Refactoring without tests: Run existing tests first to establish baseline — without a green test suite, you can't tell if your refactor …

    line 139
  10. Distinctive:

    Breaking public API contracts: Check for callers before renaming/removing — grep for all usages of any function, class, or method you're …

    line 141

≈ “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

Refactor Session

Change structure, not behavior. If behavior changes, that's a feature or fix — not a refactor.

Hard rule: Never refactor and change behavior in the same commit. Pick one.


Think Before Refactoring

Before jumping into structural changes:

  1. What is the END STATE you want? Describe the ideal structure, not just "clean up this mess."
  2. Are there multiple valid end states? Which serves the project best long-term?
  3. Is this the right time? Does this refactor block other work, or is it investment for the future?

Don't start moving code until you can describe where everything should end up.

Rule of Three before abstraction: Count concrete use cases RIGHT NOW (not hypothetical). If < 3, write the concrete version. Three similar lines of code is better than one premature abstraction. Only extract when you have 3+ real instances.


Before Starting: Get a Safety Net

  1. Write characterization tests — capture what the code currently does
  2. Run tests — they must pass before you start
  3. Commit the tests: test: add characterization tests for <module>

Now any breakage is immediately visible.


Refactor Types

TypeWhen to useExample
Extract functionBlock of code does one thing, deserves a name20-line loop → calculate_total()
RenameName doesn't describe what it actually doesdata → player_stats_by_game
Remove duplicationSame logic in 3+ places (rule of three)Extract to shared utility
Simplify conditionComplex boolean → named predicateif score > 0 and game_played → if is_valid_game(score, game_played)
Split large functionFunction does more than one thingSplit at natural seams
Flatten nestingMore than 3 levels deepEarly returns, guard clauses

The Process: One Change Per Commit

1. Make ONE structural change
2. Run tests — must still pass
3. Commit: "refactor: <what changed>"
4. Repeat

Never batch multiple refactors. One change = one commit = easy to revert if needed.


Guard Clauses: Flatten Deep Nesting

# BEFORE: hard to read with deep nesting
def process(data):
    if data:
        if data.is_valid():
            if not data.is_processed:
                do_work(data)

# AFTER: guard clauses (same behavior, flat structure)
def process(data):
    if not data:
        return
    if not data.is_valid():
        return
    if data.is_processed:
        return
    do_work(data)

Rename Safely

Rename one thing at a time. After renaming, verify all usages updated:

grep -r "old_name" .    # find any remaining references

Fix every usage before committing. IDE refactor tools are safer than manual find-replace.


When to STOP

Stop immediately if:

  • You find a bug → fix it in a separate fix: commit, then resume
  • You want to add a feature → do it in a separate feat: commit
  • Tests start failing and you're not sure why → git diff to see what changed, revert the last step
  • The refactor keeps growing → stop, open a TODO, scope it properly first

Quick Reference

SituationWhat to do
Found a bug while refactoringStop. git stash refactor, fix bug, then restore.
Tests fail after renamegrep -r "old_name" . — find missed references
Function getting complexExtract smaller pieces before touching names
Unsure if behavior changedYour characterization tests will tell you
Refactor feels like a rewriteStop — you're redesigning, not refactoring
Tempted to add a featureOpen a TODO. Finish the refactor first.

Out of Scope

  • NOT for fixing bugs or changing behavior — use a debug skill for bugs, a TDD skill for new behavior
  • NOT for adding new features while restructuring — use a spec-driven-development skill for feature work
  • NEVER use this for full rewrites or redesigns — if the refactor feels like a rewrite, scope it as a new feature instead
  • NOT for code review of someone else's work — use a code-review skill instead

Common Traps

  • Refactoring without tests: Run existing tests first to establish baseline — without a green test suite, you can't tell if your refactor broke behavior.
  • Scope creep during refactoring ("while I'm here..."): Stick to the stated refactoring goal — mixing refactoring with feature work or unrelated cleanup creates hard-to-review commits and increases revert risk.
  • Breaking public API contracts: Check for callers before renaming/removing — grep for all usages of any function, class, or method you're changing. External consumers won't get your rename for free.

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.