refactor-master
Skill0 starsChanged 6 months agoMITView on GitHub
Restructure messy code into smaller, more focused pieces — without changing observable behavior. Knows when to extract a function, split a module, replace a conditional with a strategy, pull pure logic out of side-effect-heavy code, and when to do nothing. Use when the user says "refactor this", "clean this up", "this file is too big", "split this module", "make this readable", or hands over a god-class and asks for a rescue plan.
In this repository16 other files it gives its agents.
- for-codex/AGENTS.mdAGENTS.md
- api-architectSkill
- browser-qaSkill
- code-auditorSkill
- doc-craftSkill
- git-flow-proSkill
- mcp-forgeSkill
- perf-hunterSkill
- resilience-engineerSkill
- schema-architectSkill
- security-sentinelSkill
- ship-itSkill
- test-architectSkill
- threat-modelerSkill
- type-tightenerSkill
- ui-polishSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
14 rules. About 0 are common, 11 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈11
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 17
Do *not* trigger for: behavior changes (that's a feature, not a refactor), performance work (use perf-hunter), or a one-line rename (just …
- Distinctive: line 32
Read the target. Name the *specific* smell, out loud, before touching:
- Distinctive: line 42
If you can't name the smell, the file probably doesn't need refactoring.
- Distinctive: line 48
Run the existing tests. Note which pass.
- Distinctive: line 49
If coverage is thin, write a characterization test — one test that pins the current behavior for the path you're about to change.
- Distinctive: line 102
Run the test suite. If anything fails that wasn't failing before, the refactor changed behavior.
- Distinctive: line 113
Use git mv (not delete + create) when moving files, so blame survives.
- Distinctive: line 114
Keep public APIs stable. Refactor the internals, leave the import path alone.
- Distinctive: line 115
Delete dead code aggressively.
- Distinctive: line 118
Don't refactor without tests.
- Distinctive: line 119
Don't combine a refactor and a feature in one PR.
- Distinctive: line 120
Don't introduce DI containers, plugin systems, or abstract base classes to "future-proof" a 200-line file.
- Very specific: line 121
Don't rename a public function without greping every caller, including string-based ones (DI tokens, dynamic imports).
- Very specific: line 133
Stop. Ship. Note: notifications and persistence are still tangled in there.
≈ “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.
Files
refactor-master — change shape without changing behavior
When to use this skill
Trigger when the user wants the code reorganized but expects the same outputs. Strong signals:
- "refactor this", "clean up", "split", "decompose", "extract"
- "this file is 1200 lines, help"
- "make this readable"
- "pull the logic out of the React component"
Do not trigger for: behavior changes (that's a feature, not a refactor), performance work (use perf-hunter), or a one-line rename (just do it).
The output contract
Refactors that:
- Preserve behavior — every existing test still passes. If there are no tests, add a characterization test first (see step 2).
- Land in small steps — each commit is independently reviewable and reverts cleanly.
- Reduce one specific kind of complexity — say which one upfront (cyclomatic, depth, length, coupling, naming) and measure before/after.
- Don't leak new abstractions — no
BaseFooFactoryProviderInterfaceunless there are at least three concrete implementations that need it.
Workflow
1 — Diagnose
Read the target. Name the specific smell, out loud, before touching:
- Length: function/file is too long → extract by responsibility
- Depth: too many levels of nesting → early returns, guard clauses, or extract inner blocks
- Repetition: same pattern 3+ times → extract a helper
- God object: one class doing too many things → split by axis of change
- Tangled side effects: pure logic interwoven with I/O → pull out a pure core, push I/O to the edges
- Conditional explosion:
switchon atypefield with N branches → polymorphism, strategy map, or discriminated union - Bad names: function names that lie → rename before splitting, never after
If you can't name the smell, the file probably doesn't need refactoring. Stop.
2 — Safety net first
Before changing anything:
- Run the existing tests. Note which pass.
- If coverage is thin, write a characterization test — one test that pins the current behavior for the path you're about to change. It doesn't need to be pretty, just present.
- For UI components, take a screenshot via the preview tools as a visual baseline.
3 — Move in slices
Pick one smell. Address it in one commit. Don't combine refactors.
Common moves, in order of preference:
Extract function — when a block has a clear name and 3+ lines.
// before
if (user.subscription && user.subscription.status === 'active' && user.subscription.plan !== 'trial') { ... }
// after
if (isOnPaidPlan(user)) { ... }Early return — when nesting is hiding the happy path.
// before
if (a) { if (b) { if (c) { doIt() } } }
// after
if (!a) return
if (!b) return
if (!c) return
doIt()Extract module — when a file mixes two unrelated concerns. Pull one concern into its own file with a tight export surface.
Pure core / impure shell — when business logic is tangled with database/HTTP/file calls. Move the pure logic into a function that takes plain data and returns plain data. The shell stays thin.
Replace conditional with table — when a switch or if/else if chain dispatches on a value.
// before
switch (kind) {
case 'image': return processImage(x)
case 'video': return processVideo(x)
...
}
// after
const processors = { image: processImage, video: processVideo, ... }
return processors[kind](x)Introduce parameter object — when a function takes 5+ args and several are usually passed together.
4 — Verify after every slice
After each commit:
- Run the test suite. If anything fails that wasn't failing before, the refactor changed behavior. Revert and try again.
- For UI: re-screenshot, diff against the baseline.
5 — Stop on time
A refactor session has diminishing returns. After ~3 slices on the same file, ship what you have. Future-you can do more later.
Patterns and anti-patterns
✅ Do:
- Refactor right before adding a feature that touches the messy area. The new feature pays for the cleanup.
- Use
git mv(not delete + create) when moving files, so blame survives. - Keep public APIs stable. Refactor the internals, leave the import path alone.
- Delete dead code aggressively. Git remembers it if you need it back.
❌ Don't:
- Don't refactor without tests. You'll change behavior and not know it.
- Don't combine a refactor and a feature in one PR. The reviewer can't tell what's behavioral.
- Don't introduce DI containers, plugin systems, or abstract base classes to "future-proof" a 200-line file. YAGNI.
- Don't rename a public function without
greping every caller, including string-based ones (DI tokens, dynamic imports).
Example invocation
User: "src/checkout.ts is 800 lines and I can't find anything. Help."
- Diagnose: god module. Mixes pricing logic, tax calculation, payment provider calls, email sending, and DB writes.
- Safety net: pricing + tax have unit tests; payment + email do not. Write 2 characterization tests covering the happy-path checkout and one error path.
- Slice 1: extract
pricing.ts(pure functions only). Commit. - Slice 2: extract
tax.ts(pure). Commit. - Slice 3: extract
paymentProvider.ts(the Stripe-specific code). Commit. - Now
checkout.tsis ~300 lines and orchestrates the four pieces. Run tests — all green. - Stop. Ship. Note:
notificationsandpersistenceare still tangled in there; a follow-up refactor can split those.
See also
test-architect— to add the characterization tests safelycode-auditor— to find the smells worth refactoringperf-hunter— when the refactor is motivated by performance, not readability
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
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.

