Overview
The Refactoring Assistant identifies code smells, suggests improvements, and helps execute safe refactoring operations that improve code quality without breaking functionality.
SKILL.md Template
---
name: refactor
description: Identify refactoring opportunities and guide safe code transformations. Use when improving code quality, reducing technical debt, or restructuring code.
---
When refactoring code, follow this approach:
## 1. Code Smell Detection
Identify common issues:
### Complexity Smells
- Long methods (>20 lines)
- Deep nesting (>3 levels)
- Long parameter lists (>4 params)
- Complex conditionals
### Duplication Smells
- Copy-paste code blocks
- Similar class structures
- Repeated logic patterns
### Coupling Smells
- Feature envy
- Inappropriate intimacy
- Middle man classes
- Message chains
## 2. Refactoring Techniques
### Extract Method
Before:
```javascript
function processOrder(order) {
// validate
if (!order.items) throw new Error('No items');
if (!order.customer) throw new Error('No customer');
// calculate
let total = 0;
for (const item of order.items) {
total += item.price * item.quantity;
}
// apply discount
if (order.customer.isPremium) {
total *= 0.9;
}
return total;
}
After:
function processOrder(order) {
validateOrder(order);
const total = calculateTotal(order.items);
return applyDiscount(total, order.customer);
}
Replace Conditional with Polymorphism
Introduce Parameter Object
Replace Magic Numbers with Constants
3. Safety Checklist
Before refactoring:
- Tests exist and pass
- Understand the full scope
- Identify all callers
- Plan incremental changes
After refactoring:
- All tests still pass
- No behavior changes
- Code review completed
- Performance verified
## Example Analysis
```markdown
## Refactoring Analysis: UserService.js
### Code Smells Found
1. **Long Method** (Line 45-120)
- `processUserRegistration` is 75 lines
- Suggestion: Extract validation, notification, and persistence
2. **Duplicated Code** (Lines 150, 220)
- Email validation repeated in two places
- Suggestion: Extract to shared utility
3. **Feature Envy** (Line 180)
- `calculateUserScore` accesses too many fields from `statsService`
- Suggestion: Move method to StatsService
### Recommended Refactorings
| Priority | Smell | Technique | Effort |
|----------|-------|-----------|--------|
| High | Long Method | Extract Method | Medium |
| High | Duplication | Extract Function | Low |
| Medium | Feature Envy | Move Method | Medium |
### Step-by-Step Plan
1. Add tests for `processUserRegistration` (if missing)
2. Extract `validateRegistration()` method
3. Extract `sendWelcomeEmail()` method
4. Extract `createUserRecord()` method
5. Run tests, verify behavior unchanged
Usage
/refactor src/services/UserService.ts
Or ask naturally:
How can I improve the code quality in this file?
Best Practices
- Refactor in small, testable increments
- Never refactor and add features simultaneously
- Use version control to checkpoint progress
- Keep tests green throughout the process