Refactor
Goal
Change structure, never behaviour. Every step leaves the code working, and the stated goal (easier to read, test or change) is reached without extra polishing.
When to use
- The user asks to refactor, clean up, simplify or restructure code.
- For fixing a bug, use
use_skill with debugging.
- For adding tests to code before changing it, use
use_skill with write-tests.
- For adding behaviour, use
use_skill with add-feature.
Steps
- Read the code and its callers. Use
rg for usages and explore for broader questions.
- Find the tests that cover the code and run them with
run_command to confirm they pass now. If coverage is thin and the code is risky, propose characterisation tests first, which pin down current behaviour even when it is odd.
- State the goal in one line: what will be easier after this (reading, testing, changing X).
- For more than a couple of steps, write the plan with
update_plan.
- Apply techniques, smallest first:
- Rename unclear variables, functions and files to say what they do.
- Extract a function from a long block that does one identifiable thing.
- Replace duplicated logic with one shared function, but only when the copies mean the same thing.
- Simplify conditionals with early returns, guard clauses, named booleans, or lookup tables instead of long if-else chains.
- Remove dead code, unused parameters and stale comments, confirming they are unused with
rg.
- Separate pure logic from I/O so it can be tested.
- Move code to where it belongs and update imports.
- Make one kind of change at a time, then run the tests again.
- Stop when the stated goal is reached.
Gotchas
- Mixing a refactor with a bug fix or new feature makes failures impossible to attribute. If you find a bug, note it and ask.
- Public interfaces stay the same unless the user agreed to change them. If they change, update every caller.
- Merging two similar-looking blocks that mean different things creates a coupling bug.
- Introducing a new pattern the project does not use costs more than the cleanup gains. Match the existing style.
- Without passing tests before you start, you cannot tell whether you broke anything.
Output
What changed and why, the files touched, confirmation that tests pass with the command used, and anything that behaves differently on purpose (nothing, unless agreed).
1---2name: refactor3description: "Improves the structure of existing code without changing its behaviour, in small verified steps: extracting functions, removing duplication, clarifying names, simplifying conditionals. Use when the user asks to refactor, clean up, simplify, restructure or tidy code."4metadata:5 source: https://skillmd.com/skills/lucki74/refactor6---78# Refactor910## Goal1112Change structure, never behaviour. Every step leaves the code working, and the stated goal (easier to read, test or change) is reached without extra polishing.1314## When to use1516- The user asks to refactor, clean up, simplify or restructure code.17- For fixing a bug, use `use_skill` with `debugging`.18- For adding tests to code before changing it, use `use_skill` with `write-tests`.19- For adding behaviour, use `use_skill` with `add-feature`.2021## Steps22231. Read the code and its callers. Use `rg` for usages and `explore` for broader questions.242. Find the tests that cover the code and run them with `run_command` to confirm they pass now. If coverage is thin and the code is risky, propose characterisation tests first, which pin down current behaviour even when it is odd.253. State the goal in one line: what will be easier after this (reading, testing, changing X).264. For more than a couple of steps, write the plan with `update_plan`.275. Apply techniques, smallest first:28 - Rename unclear variables, functions and files to say what they do.29 - Extract a function from a long block that does one identifiable thing.30 - Replace duplicated logic with one shared function, but only when the copies mean the same thing.31 - Simplify conditionals with early returns, guard clauses, named booleans, or lookup tables instead of long if-else chains.32 - Remove dead code, unused parameters and stale comments, confirming they are unused with `rg`.33 - Separate pure logic from I/O so it can be tested.34 - Move code to where it belongs and update imports.356. Make one kind of change at a time, then run the tests again.367. Stop when the stated goal is reached.3738## Gotchas3940- Mixing a refactor with a bug fix or new feature makes failures impossible to attribute. If you find a bug, note it and ask.41- Public interfaces stay the same unless the user agreed to change them. If they change, update every caller.42- Merging two similar-looking blocks that mean different things creates a coupling bug.43- Introducing a new pattern the project does not use costs more than the cleanup gains. Match the existing style.44- Without passing tests before you start, you cannot tell whether you broke anything.4546## Output4748What changed and why, the files touched, confirmation that tests pass with the command used, and anything that behaves differently on purpose (nothing, unless agreed).