â /refactor
Restructure code to improve quality without changing behavior
Command Usage
Invoke this command in Claude Code:
/refactordescription: Restructure code to improve quality without changing behavior disable-model-invocation: false
Improve code structure, readability, and maintainability without changing external behavior.
Process
Use the refactoring skill from bushido to:
- Ensure tests exist: Must have tests before refactoring (if not, add them first)
- Identify code smell: What needs improvement?
- Plan refactoring: What pattern/structure would be better?
- Make one change: Small, focused refactorings
- Run tests: Verify behavior unchanged
- Repeat: Continue with next improvement
- Review: Apply code-reviewer skill to final result
Refactoring Golden Rules
Safety first:
- â Tests exist and pass before starting
- â Make one change at a time
- â Run tests after each change
- â Behavior must remain unchanged
- â Commit after each successful refactoring
When to refactor:
- Code is hard to understand
- Duplication exists
- Functions are too long
- Classes have too many responsibilities
- Complexity is high
When NOT to refactor:
- No tests exist (add tests first)
- Under time pressure (defer to later)
- External behavior needs to change (that's not refactoring)
Common Refactorings
Extract Function:
// Before
function processOrder(order) {
const tax = order.subtotal * 0.08
const shipping = order.items.length > 5 ? 0 : 9.99
const total = order.subtotal + tax + shipping
return total
}
// After
function processOrder(order) {
const tax = calculateTax(order.subtotal)
const shipping = calculateShipping(order.items)
return order.subtotal + tax + shipping
}
function calculateTax(subtotal) {
return subtotal * 0.08
}
function calculateShipping(items) {
return items.length > 5 ? 0 : 9.99
}
Eliminate Duplication:
// Before
function formatUserName(user) {
return `${user.firstName} ${user.lastName}`
}
function formatAuthorName(author) {
return `${author.firstName} ${author.lastName}`
}
// After
function formatFullName(person) {
return `${person.firstName} ${person.lastName}`
}
Simplify Conditionals:
// Before
if (user.role === 'admin' || user.role === 'moderator' || user.role === 'super_admin') {
// ...
}
// After
const PRIVILEGED_ROLES = ['admin', 'moderator', 'super_admin']
if (PRIVILEGED_ROLES.includes(user.role)) {
// ...
}
Examples
When the user says:
- "This function is too long and hard to understand"
- "Clean up this messy code"
- "Remove duplication between these modules"
- "Simplify this nested if/else logic"
- "Break this god class into smaller pieces"
Refactoring Workflow
# 1. Ensure tests pass
npm test
# 2. Make ONE refactoring change
# (extract function, rename, remove duplication, etc.)
# 3. Run tests again
npm test
# 4. Commit
git add .
git commit -m "refactor: extract calculateTax function"
# 5. Repeat for next change
Output Format
After refactoring:
## Refactoring: [Brief description]
### Before
[Description of code smell or issue]
### Changes Made
- [Change 1 with reasoning]
- [Change 2 with reasoning]
- [Change 3 with reasoning]
### After
[How the code is better now]
### Verification
[Evidence that behavior unchanged - use proof-of-work skill]
- All tests pass: [test output]
- No functionality changed
- Code is more [readable/maintainable/simple]
Notes
- Use TodoWrite to track refactoring steps
- Apply boy-scout-rule skill (leave code better than found)
- Apply simplicity-principles skill (KISS, YAGNI)
- Apply structural-design-principles as appropriate
- Use proof-of-work skill to verify tests still pass
- Commit after each successful refactoring
- If tests don't exist, use /test command first