| name | refactor |
| description | Refactoring process with test safety. Invoke immediately when user or document mentions refactoring, or proactively when code gets too complex or messy. |
| triggers | ["refactor this code","refactor the code","refactoring code","clean up code","extract method"] |
| allowed-tools | Read Glob Write Bash |
Refactoring Production Code
Work autonomously as much as possible. Start with the simplest thing or file and proceed to the more complex ones.
Stages
- Prep
- Main Refactoring
- Final Evaluation
- Summary
Test Code Policy
Do not change test code during refactoring, except:
- Renames that follow production code renames (imports, function calls)
- Import path updates if something moved
Never change test assertions, test data, or test logic.
1. Prep
2. Main Refactoring
Code Style
Prefer self-explanatory, readable code over comments.
- Use functional helper methods for clarity
- Remove dead code
- Extract paragraphs into methods
- Use better variable names
- Remove unused imports
- Remove unhelpful local variables
- Look for opportunities to simplify
- Use domain language - name things for what they ARE, not how they're implemented
- Keep consistent abstraction levels within methods
Example — extract method and rename variable:
Before:
const x = order.items.reduce((sum, i) => sum + i.price, 0);
return x * (1 - order.dc);
After:
const subtotal = sumItemPrices(order.items);
return subtotal * (1 - order.discountRate);
function sumItemPrices(items) {
return items.reduce((sum, item) => sum + item.price, 0);
}
Example — remove dead code and use domain language:
Before:
function process(u, t) {
if (false) { return legacyProcess(u, t); }
return chargeCard(u.card, t.amount);
}
After:
function processPayment(user, transaction) {
return chargeCard(user.card, transaction.amount);
}
Process
For each refactor:
- Ensure all tests pass
- Choose and perform the simplest possible refactoring (one at a time)
- Ensure all tests pass after the change
- Commit each successful refactor with the message format: "- r " (the message must include the "- r" prefix)
Prefer small granular commits. If applying the same refactoring pattern to multiple locations, change one location at a time and commit each separately.
- Append to
REFACTORING_SUMMARY.md:
### Step {N}: {refactoring description}
- Commit: `- r {message}`
- Test result: {PASS with test count}
- Files changed: {list}
- Provide a status update after each refactor
3. Final Evaluation
When you see no more obvious refactoring opportunities, say "Entering final evaluation."
Shift focus: you've been implementing. Now become a critic. Your job is to find problems, not produce code.
Re-read Code Style guidelines. Look at each file in scope. Consider blind spots - what improvements haven't we even considered that would make the code better, easier, more maintainable?
For each file, find ONE thing that could be better. If you find something:
- Fix it using the same refactoring process (test, change, test, commit)
- Look again; fixing one thing often reveals the next
Repeat until you find nothing more to improve.
4. Summary
Provide a high-level summary of the refactoring:
- List each file that was touched
- Describe the key improvements made in each file
Append a final section to REFACTORING_SUMMARY.md:
## Final
- Total steps: {N}
- Files touched: {list of all files changed}
- All commits: {list of commit messages}
- Final test result: {PASS with test count}
This skill was originally adapted from Llewellyn Falco's refactoring process. See credits.md for attribution.