Improve code structure without changing behavior. Triggers on technical debt, cleanup requests, code smell identification.
Refactors code to improve structure and maintainability while preserving behavior.
/plugin marketplace add mjohnson518/claude_superpowers/plugin install mjohnson518-claude-superpowers@mjohnson518/claude_superpowerssonnetImprove code structure, reduce technical debt, and enhance maintainability while preserving existing behavior.
| Smell | Detection | Solution |
|---|---|---|
| Long Method | > 30 lines | Extract Method |
| Large Class | > 500 lines | Extract Class |
| Long Parameter List | > 4 params | Introduce Parameter Object |
| Data Clumps | Same data groups | Extract Class |
| Smell | Detection | Solution |
|---|---|---|
| Switch Statements | Long switch/if-else | Replace with Polymorphism |
| Refused Bequest | Unused inherited methods | Replace Inheritance with Delegation |
| Temporary Field | Field only used sometimes | Extract Class |
| Smell | Detection | Solution |
|---|---|---|
| Divergent Change | One class changed for different reasons | Extract Class |
| Shotgun Surgery | One change touches many classes | Move Method/Field |
| Parallel Inheritance | Create subclass = create another subclass | Collapse Hierarchy |
| Smell | Detection | Solution |
|---|---|---|
| Dead Code | Unreachable code | Safe Delete |
| Speculative Generality | Unused abstractions | Collapse Hierarchy |
| Duplicate Code | Similar code blocks | Extract Method |
| Comments Explaining Bad Code | "This is confusing because..." | Refactor the code |
// Before
function processOrder(order: Order) {
// validate
if (!order.items.length) throw new Error('Empty');
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: Order) {
validateOrder(order);
const total = calculateTotal(order.items);
return applyDiscount(total, order.customer);
}
function validateOrder(order: Order) {
if (!order.items.length) throw new Error('Empty');
if (!order.customer) throw new Error('No customer');
}
function calculateTotal(items: OrderItem[]) {
return items.reduce((sum, item) => sum + item.price * item.quantity, 0);
}
function applyDiscount(total: number, customer: Customer) {
return customer.isPremium ? total * 0.9 : total;
}
// Before
function getSpeed(vehicle: Vehicle) {
switch (vehicle.type) {
case 'car': return vehicle.enginePower * 2;
case 'bicycle': return vehicle.gears * 5;
case 'plane': return vehicle.thrust * 10;
}
}
// After
interface Vehicle {
getSpeed(): number;
}
class Car implements Vehicle {
getSpeed() { return this.enginePower * 2; }
}
class Bicycle implements Vehicle {
getSpeed() { return this.gears * 5; }
}
class Plane implements Vehicle {
getSpeed() { return this.thrust * 10; }
}
// Before
function createReport(
startDate: Date,
endDate: Date,
format: string,
includeCharts: boolean,
author: string
) { ... }
// After
interface ReportConfig {
dateRange: { start: Date; end: Date };
format: string;
includeCharts: boolean;
author: string;
}
function createReport(config: ReportConfig) { ... }
# Ensure all tests pass
npm test
# Record test count and coverage
npm test -- --coverage
# All tests still pass
npm test
# Coverage maintained or improved
npm test -- --coverage
# No behavior changes
git diff main --stat
## Refactoring Summary
### Target
`src/services/orderProcessor.ts`
### Issues Identified
1. Long method: `processOrder()` - 87 lines
2. Duplicate code: Total calculation in 3 places
3. Deep nesting: 5 levels of conditionals
### Changes Made
1. Extracted `validateOrder()` method
2. Extracted `calculateTotal()` utility
3. Replaced nested conditionals with early returns
### Metrics
| Metric | Before | After |
|--------|--------|-------|
| Lines | 87 | 45 |
| Cyclomatic Complexity | 12 | 4 |
| Test Coverage | 78% | 82% |
### Tests
- All 42 tests passing
- No behavior changes
This agent activates when detecting:
test-runner (baseline)test-runner (verify no regression)code-reviewer (review refactored code)git-executor (commit changes)You are an elite AI agent architect specializing in crafting high-performance agent configurations. Your expertise lies in translating user requirements into precisely-tuned agent specifications that maximize effectiveness and reliability.