agentleFS
jmolzjmolz/m0lz.01/.agents/skills/refactor/SKILL.md

refactor

Skill0 starsChanged 6 months agoMITView on GitHub

Use this skill when restructuring, simplifying, or improving existing code without changing behavior. Triggers: 'refactor', 'simplify', 'clean up', 'reorganize', 'extract', 'consolidate', 'reduce duplication', 'too complex', 'tech debt', '/simplify'. Provides safe refactoring patterns that preserve behavior while improving structure.

196 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use ascii-diagram, debug-fix and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • SethGammonSkill

    ascii-diagram

    Generate perfectly aligned ASCII diagrams — architecture, flow, sequence, box-and-arrow. Uses a programmatic character-grid approach so alignment is guaranteed by math, not token prediction. Includes post-render verification.

    Also in 4 projects that use this

  • zebbernSkill

    debug-fix

    Find and fix a bug or issue — from any source (GitHub issue, error message, user report, or observed behavior)

    Also in 4 projects that use this

  • parcadeiSkill

    agent-context-isolation

    Agent Context Isolation

    Also in 3 projects that use this

  • parcadeiSkill

    agentica-infrastructure

    Reference guide for Agentica multi-agent infrastructure APIs

    Also in 3 projects that use this

  • +8 more from parcadei

The file

Show its rules and text

We split this skill into the rules it gives an agent

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

  • 0

    Common

    Most of the 169 edited versions we read keep these.

  • 14

    Distinctive

    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:

    Tests must pass before AND after refactoring: If there are no tests, write them first.

    line 12
  2. Distinctive:

    Run the full test suite — confirm everything passes

    line 16
  3. Distinctive:

    Commit current state — create a clean rollback point

    line 17
  4. Distinctive:

    Update imports throughout the codebase

    line 34
  5. Distinctive:

    Create a single source of truth (utility, hook, base class)

    line 40
  6. Distinctive:

    Use your IDE's rename tool, not find-and-replace

    line 55
  7. Distinctive:

    Check that all callers updated correctly

    line 56
  8. Distinctive:

    If a wrapper just passes through to one function, remove the wrapper

    line 60
  9. Distinctive:

    If an interface has only one implementation and no plans for more, consider inlining

    line 61
  10. Distinctive:

    Small steps, frequent tests: Never make multiple structural changes between test runs.

    line 76
  11. Distinctive:

    Run full test suite + validation

    line 88
  12. Distinctive:

    Check that no public API changed (unless intentional)

    line 89
  13. Distinctive:

    Update any documentation that references changed structure

    line 90
  14. Distinctive:

    Consider updating AGENTS.md or rules if patterns changed

    line 91

“Common” is measured: kept by at least 6 in 10 of the 169 edited versions of this file we could read. “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 Skill

When to Use

The agent should read this skill when restructuring code to improve quality without changing external behavior.

Golden Rule

Tests must pass before AND after refactoring. If there are no tests, write them first. Refactoring without tests is just changing code and hoping.

Pre-Refactor Checklist

  1. Run the full test suite — confirm everything passes
  2. Commit current state — create a clean rollback point
  3. Identify the specific problem (don't just "clean up")
  4. Plan the changes before making them

Common Refactoring Patterns

Extract Function

When: A block of code does one distinct thing inside a larger function.

# Before: 50-line function doing 3 things
# After: 3 focused functions, each 15-20 lines

Extract Module/File

When: A file has grown past 300-400 lines or handles multiple concerns.

  • Group related functions and types
  • Move to a new file with a clear name
  • Update imports throughout the codebase
  • Verify nothing broke

Consolidate Duplication

When: Similar logic exists in 3+ places.

  • Identify the shared pattern
  • Create a single source of truth (utility, hook, base class)
  • Replace duplicates with calls to the shared version
  • Don't consolidate things that are only superficially similar — if they'll diverge, keep them separate

Simplify Conditionals

When: Nested if/else blocks are hard to follow.

  • Early returns to reduce nesting
  • Guard clauses at the top of functions
  • Lookup objects/maps instead of long switch statements
  • Named boolean variables for complex conditions

Rename for Clarity

When: Names don't communicate intent.

  • Functions: verb + noun (getUserById, not getData)
  • Booleans: isActive, hasPermission, shouldRetry
  • Use your IDE's rename tool, not find-and-replace
  • Check that all callers updated correctly

Flatten Hierarchy

When: Too many layers of abstraction for no benefit.

  • If a wrapper just passes through to one function, remove the wrapper
  • If an interface has only one implementation and no plans for more, consider inlining
  • Premature abstraction is worse than duplication

Refactoring Workflow

1. Identify → What specifically is the problem?
2. Test     → Ensure existing tests pass (write them if missing)
3. Commit   → Clean rollback point
4. Change   → Make ONE refactoring move at a time
5. Test     → Run tests after each move
6. Commit   → Checkpoint after each successful move
7. Repeat   → Next refactoring move

Small steps, frequent tests. Never make multiple structural changes between test runs.

When NOT to Refactor

  • ❌ Right before a deadline (refactor after shipping)
  • ❌ Code you don't understand yet (understand first, then refactor)
  • ❌ Code that has no tests and is working (add tests first)
  • ❌ For aesthetic reasons alone (must improve maintainability)
  • ❌ Everything at once (pick the highest-impact area)

Post-Refactor

  1. Run full test suite + validation
  2. Check that no public API changed (unless intentional)
  3. Update any documentation that references changed structure
  4. Consider updating AGENTS.md or rules if patterns changed

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.