agentleFS
svyatovsvyatov/supermatt/skills/refactor/SKILL.md

refactor

Skill20 starsChanged 51 days agoMITView on GitHub

Refactor code at method, file, or project scope. Assesses first and stops when the code is already clean, routes architecture problems elsewhere, and gates edits on test coverage. Language-agnostic.

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

20 rules. 0 are common, 18 are distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 169 edited versions we read keep these.

  • 18

    Distinctive

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Less total code: If "better organized" means more lines, it's more entropy, not less.

    line 27
  2. Distinctive:

    Read a reference only when the assessment actually turns on it:

    line 30
  3. Distinctive:

    This step is mandatory: Read the target code and evaluate it against the criteria below.

    line 51
  4. Distinctive:

    All method-level checks, plus:

    line 63
  5. Distinctive:

    All file-level checks, plus:

    line 71
  6. Distinctive:

    Idempotency rule: if you run this skill on the same code twice and it's already been refactored, the verdict must be Clean.

    line 96
  7. Distinctive:

    Search for test files covering the target code (look for test files matching the module/class name, grep for the function name in test …

    line 157
  8. Distinctive:

    Do not skip this step. Refactoring means "change structure, preserve behavior", and you can only guarantee "preserve behavior" if there are …

    line 165
  9. Distinctive:

    One concern at a time: don't mix refactoring with feature work.

    line 173
  10. Distinctive:

    Bias toward deletion: every refactoring is a chance to remove code.

    line 174
  11. Distinctive:

    Tests are frozen: do not edit test files to make them pass.

    line 175
  12. Very specific:

    Start from a clean, committed baseline: commit or stash unrelated work first, then record the current commit as <baseline>.

    line 176
  13. Distinctive:

    Revert, don't debug forward: when a step goes red, undo it and take a smaller one.

    line 178
  14. Very specific:

    When one move repeats across more than a handful of sites, read MECHANICAL-SWEEPS.md before editing by hand.

    line 184
  15. Distinctive:

    Delete dead code: unused functions, imports, variables, commented-out blocks

    line 204
  16. Distinctive:

    Prefer data structures over custom types when custom behavior isn't needed

    line 206
  17. Distinctive:

    Ask: could this be fewer functions?

    line 207
  18. Distinctive:

    If the metric improved but the code reads worse, the transformation failed.

    line 215
  19. Distinctive:

    When refactoring a specific method for complexity:

    line 219
  20. Distinctive:

    Use guard clauses to reduce nesting depth.

    line 223

“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

Improve code structure without changing behavior. Works at three scopes: project, file, or method. Starts by assessing whether refactoring is even warranted: if the code is already clean, say so and stop.

Refactoring vs architecture: this skill handles code-level improvements: complexity, duplication, naming, dead code, method extraction. If the assessment shows that the real problem is module boundaries, coupling between packages, or shallow modules that need deepening, say so. That is an architecture problem this skill won't fix: tell the user to run /improve-architecture in Claude linked installs or Cursor, /supermatt:improve-architecture from the Claude plugin, $improve-architecture in Codex, or /skill:improve-architecture in OMP.

Philosophy

These principles calibrate what "good enough" means and guide every step below:

  • Measure the end state, not the effort. Writing 50 lines that delete 200 is a win.
  • Simple over easy. Simple means one concept, not intertwined. Easy means familiar. Choose simple.
  • Separate, don't combine. Good design is taking things apart: removing dependencies, not adding abstractions.
  • Data over abstractions. 100 functions on one data structure beat 10 functions on 10 custom types.
  • Less total code. If "better organized" means more lines, it's more entropy, not less.
  • When in doubt, leave it alone. The default verdict is Clean. Don't invent work.

Read a reference only when the assessment actually turns on it:

  • SIMPLICITY-VS-EASY.md: when the choice is between a familiar approach and a simpler one
  • DATA-OVER-ABSTRACTIONS.md: when custom types could be plain data structures
  • DESIGN-IS-TAKING-APART.md: when a coupled system needs decomposing
  • EXPENSIVE-TO-ADD-LATER.md: when deciding whether something missing is worth adding now

Step 1: Determine scope

Infer from the user's request:

TriggerScope
"refactor this method/function" or a specific symbol namemethod
"refactor this file" or a file pathfile
"refactor this project/codebase" or no specific targetproject

"The changes on this branch since <ref>" is file scope over each file in git diff --name-only <ref>...HEAD, judged on the changed code. If the scope is still ambiguous, ask the user, with the three scopes as the options.

Step 2: Assess whether refactoring is warranted

This step is mandatory. Read the target code and evaluate it against the criteria below. The default verdict is Clean: only escalate when there's clear evidence.

Method scope

  • Cognitive complexity: nesting depth >3, boolean expressions with >2 operators, or branch count that makes the flow hard to follow. Investigate, but a long method that reads linearly is not automatically a smell.
  • Length: functions over ~50 lines warrant a look, but length alone isn't a verdict. A 70-line function with clear flow is better than 5 tiny functions that scatter the logic.
  • Parameter count: >4 parameters on a public API boundary suggests grouping. Internal helpers can have more.
  • Single responsibility: does it do one thing?
  • Naming clarity: do names describe intent or mechanics?

File scope

All method-level checks, plus:

  • Dead code (unused functions, imports, variables)
  • Duplicated logic across functions in the same file
  • God class or kitchen-sink module: too many unrelated responsibilities
  • Cohesion: do all parts belong together?

Project scope

All file-level checks, plus:

  • Circular dependencies between modules
  • Duplicated logic across files/modules
  • Overly deep module nesting or excessive file count for what the code does
  • Total codebase size: could fewer files/functions achieve the same result?

A whole codebase always offers more findings than are worth acting on. Read HOTSPOTS.md and rank by change frequency crossed with complexity before listing anything. It decides what to look at first, not what is wrong.

Read the ranked candidates through subagents, one per area, each returning findings rather than file contents. A project-scope assessment reads far more code than it reports on, and most of it comes back Clean. Step 4 reads what it actually edits. A project under about 1,000 source lines is the exception: read it directly, a few files per call, so each output stays under the 30,000-character limit that moves a result to a file.

Planned and settled work

Before you list any finding, read each issue the target's commits reference, with its comments, through the Read an issue operation in docs/agents/issue-tracker.md. Drop a finding that a comment already settled, such as a user's answer to the same question during implement, and name that comment in the verdict.

A finding that removes an indirection (a function where a value would do, a parameter, a hook, a seam) can take out what the next planned change needs. Before you list one, also read the open children of those issues' parent issue. Drop a finding that a planned issue needs, and name that issue in the verdict.

The verdict

RatingMeaningAction
CleanNo meaningful improvements availableTell the user the code looks good and end the run there. Stop here unless they provide a specific reason to continue. An item worth offering the user makes the verdict Minor.
MinorSmall improvements possible (naming, minor duplication)List findings. Recommend applying them or leaving the code as is, give the deciding reason, then ask. These are optional.
SignificantClear code smells or structural issuesList findings with priorities. Proceed to Step 3.
CriticalMajor structural problems blocking maintainabilityList findings urgently. Proceed to Step 3.

Idempotency rule: if you run this skill on the same code twice and it's already been refactored, the verdict must be Clean.

User override: if the user provides a specific reason to refactor code rated Clean (e.g., "I know it looks fine but I want to extract this into smaller functions"), proceed with their request, but scope the changes strictly to what they asked for. Don't expand into a broader refactoring.

Architecture signal: if the assessment shows problems that are really about module boundaries (tightly coupled packages, shallow modules, missing abstractions at the architectural level), say so: refactoring code inside a badly-bounded module won't fix the real problem. Tell the user to run /improve-architecture in Claude linked installs or Cursor, /supermatt:improve-architecture from the Claude plugin, $improve-architecture in Codex, or /skill:improve-architecture in OMP.

Step 3: Plan

For method scope, skip to Step 3.5. Planning is unnecessary for single-symbol changes, but the test gate still applies.

Enter plan mode, where the host has one, before planning, so no file changes while the plan is drafted. If the request is too ambiguous to plan safely, ask rather than start.

For file scope with 1-3 changes, a bullet list is sufficient:

Changes:
- Extract X into helper function
- Remove unused imports
- Rename Y to describe intent

For file scope with 4+ changes or project scope, produce a structured plan:

## Refactor Plan: [title]

### Current State
[What's wrong and why it matters]

### Target State
[What the code looks like after: fewer lines, fewer concepts, clearer boundaries]

### Changes
| File | Action | Why | Blocked by |
|------|--------|-----|------------|
| path | modify/create/delete | reason | dependencies |

### Sequence
Phase 1: [contracts/types/interfaces if applicable]
Phase 2: [implementation changes]
Phase 3: [callers]
Phase 4: [test updates, mechanical renames and moves only]
Phase 5: [cleanup, delete dead code]

Verify after each phase: [command]
Final validation: [command that must pass before this is done]

### Rollback
For the riskiest phase: [how to undo it]

### Risks
- [What could go wrong and how to mitigate]

Key principle: the target state should have less or equal total code. If your plan adds net lines without removing complexity, reconsider. "Better organized but more code" is not a win: it's more entropy.

Present the plan through the host's native approval mechanism when exposed, otherwise in chat. Proceed only after explicit approval.

Step 3.5: Verify test coverage

Before changing any code, check that tests exist for the target:

  1. Search for test files covering the target code (look for test files matching the module/class name, grep for the function name in test directories).
  2. If tests exist, note which behaviors they cover and run the affected tests against the unchanged code. Record the command and result as the baseline. If it fails or flakes, stop and report the failure; resolve it or agree its handling with the user before editing. Do not attribute an existing failure to a refactor or use it as regression evidence. Proceed to Step 4 only with a usable baseline.
  3. If tests are missing or sparse, stop and tell the user. Refactoring without tests risks silently changing business logic. Ask whether to write characterization tests first or proceed without them. To write them, load and follow the tdd skill through the host's skill mechanism and use its characterization tests in their own test: commit.

Judge coverage over the code you are about to change, not over the repository. A repo at 85% overall tells you nothing if the target sits in the untested 15%.

This gate is especially important for code with business logic (calculations, validation rules, state transitions, money). Structural code (routing, configuration, glue) is lower-risk without tests, but still worth flagging.

Do not skip this step. Refactoring means "change structure, preserve behavior", and you can only guarantee "preserve behavior" if there are tests to prove it.

Step 4: Refactor

Ground rules

  1. Behavior is preserved: refactoring changes structure, not behavior.
  2. Small steps: one change at a time, verify after each.
  3. One concern at a time: don't mix refactoring with feature work.
  4. Bias toward deletion: every refactoring is a chance to remove code. Ask: what does this make obsolete?
  5. Tests are frozen: do not edit test files to make them pass. A test that has to change is telling you the behavior changed, which means this is no longer a refactor. Mechanical renames and file moves are the only exception.
  6. Start from a clean, committed baseline: commit or stash unrelated work first, then record the current commit as <baseline>. Any characterization tests from Step 3.5 must already be committed.
  7. Commit each step separately, as refactor:, and never mix structural and behavioral change in one commit: a move and an optimization are two commits even though both are structural.
  8. Revert, don't debug forward: when a step goes red, undo it and take a smaller one. The revert is free; the debugging session is not.

Common refactoring moves

Apply whichever are relevant. Do not apply moves that don't improve the specific code.

When one move repeats across more than a handful of sites, read MECHANICAL-SWEEPS.md before editing by hand.

Reduce complexity:

  • Replace nested conditionals with guard clauses or early returns
  • Extract focused helper functions from long methods
  • Replace complex boolean expressions with named predicates
  • Break god classes into cohesive modules

Eliminate duplication:

  • Extract shared logic into a single function
  • Replace copy-pasted code with parameterized functions
  • Consolidate duplicate type definitions

Improve clarity:

  • Rename variables/functions to describe intent, not mechanics
  • Replace magic numbers/strings with named constants
  • Group related parameters into objects/records
  • Move logic to the object that owns the data (fix feature envy)

Reduce entropy:

  • Delete dead code: unused functions, imports, variables, commented-out blocks
  • Inline single-use abstractions (unnecessary wrappers, thin delegators)
  • Prefer data structures over custom types when custom behavior isn't needed
  • Ask: could this be fewer functions? Fewer files? Fewer concepts?

Where deletion goes wrong

"Less code" is the measure, not the goal. Reject a transformation that wins on line count and loses on reading:

  • Nested ternaries and dense one-liners are fewer lines and harder to read. Choose clarity over brevity.
  • An abstraction that organizes the code is not the same as an unnecessary wrapper. Inline the thin delegator, keep the one that names a concept.
  • If the metric improved but the code reads worse, the transformation failed. Readability is the criterion; line count is a proxy.

Method complexity reduction

When refactoring a specific method for complexity:

  1. Identify complexity sources: nesting, long if/else chains, repeated blocks, complex booleans.
  2. Extract focused helpers, each with a single clear responsibility.
  3. Use guard clauses to reduce nesting depth.
  4. The main method should read as a high-level flow.
  5. Preserve all input/output behavior, error handling, and edge cases.

Step 5: Verify

After refactoring:

  1. Check that no test changed. Compare the baseline commit from ground rule 6 with the current tracked working-tree state: git diff --name-only <baseline> includes committed, staged, and unstaged changes. Characterization tests committed in Step 3.5 are already in the baseline; subsequent changes to them belong in that list and follow the same frozen-test rule. If an existing test file was modified beyond a rename or a move, stop and say so: the behavioral contract was rewritten to match the new code, which hides exactly the failure this step exists to catch.
  2. Run all tests that cover the refactored code. Read the actual output; a command that exited is not a suite that passed. If any fail, fix your refactoring, not the tests.
  3. Confirm behavior is unchanged: same inputs produce same outputs. If tests were written in Step 3.5, they serve as the proof.
  4. Check the result: re-evaluate the code against Step 2 criteria. The verdict should be Clean or Minor. If only Minor issues remain, note them but do not re-enter the refactoring loop.

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.