Refactor
Improve code structure without changing external behavior. The default path:
change structure in small steps, always with tests.
Not for: rewrites from scratch, or code without tests. For architectural
deepening (module interfaces, seams, testability), fold that work into the
restructure itself rather than handing off to a separate skill. Scope
discipline (how small is small enough) is a separate concern — this skill
reshapes structure, it does not shrink scope.
When to use
- Code is hard to understand or maintain.
- Functions/classes are too large.
- Adding features is difficult due to code structure.
- The user asks for a refactor plan.
Vocabulary: key terms
- Deep module — lots of behaviour behind a small interface.
- Interface — everything a caller must know to use a module correctly: type signature, invariants, ordering, error modes, config, performance.
- Seam (Feathers) — where a module's interface lives; the place you can alter behaviour without editing in place.
- Adapter — a concrete thing that satisfies an interface at a seam.
- Depth — leverage at the interface; the deletion test tells you if a module earned its keep.
For deepening clusters or exploring alternatives, see references/DEEPENING.md and references/DESIGN-IT-TWICE.md.
Local refactor
Prepare — Write or confirm tests. Commit working state. Identify the code smell.
Refactor in small steps — One change, run tests, commit if they pass. Repeat. Never mix refactoring with feature changes.
Verify — Run full test suite and type checker. Confirm no behavioral changes.
Clean up — Update comments and docs. Remove temporary markers.
Multi-file refactor
Adds a written plan step before the local refactor steps above:
- Write plan — Read the code. Identify affected files, dependencies, and hidden coupling. Sequence changes: contracts/types first, then implementations, callers, tests, cleanup. Include verification between phases and rollback for risky phases. Use
references/refactor-plan-template.md. Confirm with user before implementing.
Then follow Local refactor steps 1-4.
When NOT to use
- Greenfield feature work — use
test-driven-development + ponytail (scope discipline lives there; this skill only reshapes structure).
- Production code without tests — add tests first (use
test-driven-development), then refactor.
- Rewrites from scratch.
Constraints
- Behavior is preserved — refactoring changes structure, not behavior.
- Tests are mandatory — without tests, you're editing, not refactoring.
- One change at a time — don't bundle refactoring with feature work.
- Small steps — if a step feels large, split it further.
When not to refactor
Code that works and won't change again, or production code without tests (add tests first).
Completion criteria
Related skills
ponytail — scope: smallest slice; this skill: best structure for that slice.
test-driven-development — mandatory failing-test-first when adding behavior after refactor.
systematic-debugging — fix root cause before reshaping.
1---2name: refactor3description: Use when existing code needs restructuring without behavior change — large functions, code smells, hard-to-maintain structure, or a multi-file refactor plan with tests.4license: MIT5---67# Refactor89Improve code structure without changing external behavior. The default path:10change structure in small steps, always with tests.1112**Not for:** rewrites from scratch, or code without tests. For architectural13deepening (module interfaces, seams, testability), fold that work into the14restructure itself rather than handing off to a separate skill. Scope15discipline (how small is small enough) is a separate concern — this skill16reshapes *structure*, it does not shrink *scope*.1718## When to use1920- Code is hard to understand or maintain.21- Functions/classes are too large.22- Adding features is difficult due to code structure.23- The user asks for a refactor plan.2425## Vocabulary: key terms2627- **Deep module** — lots of behaviour behind a small interface.28- **Interface** — everything a caller must know to use a module correctly: type signature, invariants, ordering, error modes, config, performance.29- **Seam** (Feathers) — where a module's interface lives; the place you can alter behaviour without editing in place.30- **Adapter** — a concrete thing that satisfies an interface at a seam.31- **Depth** — leverage at the interface; the deletion test tells you if a module earned its keep.3233For deepening clusters or exploring alternatives, see `references/DEEPENING.md` and `references/DESIGN-IT-TWICE.md`.3435## Local refactor36371. **Prepare** — Write or confirm tests. Commit working state. Identify the code smell.38392. **Refactor in small steps** — One change, run tests, commit if they pass. Repeat. Never mix refactoring with feature changes.40413. **Verify** — Run full test suite and type checker. Confirm no behavioral changes.42434. **Clean up** — Update comments and docs. Remove temporary markers.4445## Multi-file refactor4647Adds a written plan step before the local refactor steps above:48490. **Write plan** — Read the code. Identify affected files, dependencies, and hidden coupling. Sequence changes: contracts/types first, then implementations, callers, tests, cleanup. Include verification between phases and rollback for risky phases. Use `references/refactor-plan-template.md`. Confirm with user before implementing.5051Then follow Local refactor steps 1-4.5253## When NOT to use5455- Greenfield feature work — use `test-driven-development` + `ponytail` (scope discipline lives there; this skill only reshapes structure).56- Production code without tests — add tests first (use `test-driven-development`), then refactor.57- Rewrites from scratch.5859## Constraints6061- **Behavior is preserved** — refactoring changes structure, not behavior.62- **Tests are mandatory** — without tests, you're editing, not refactoring.63- **One change at a time** — don't bundle refactoring with feature work.64- **Small steps** — if a step feels large, split it further.6566### When not to refactor6768Code that works and won't change again, or production code without tests (add tests first).6970## Completion criteria7172- [ ] Code smell named and scoped to local or multi-file plan73- [ ] Tests present and green before first structural edit74- [ ] Each step is one edit → tests green → commit75- [ ] Full test suite + type checker green, behavior unchanged7677## Related skills7879- `ponytail` — scope: smallest slice; this skill: best structure for that slice.80- `test-driven-development` — mandatory failing-test-first when adding behavior after refactor.81- `systematic-debugging` — fix root cause before reshaping.