cosmicstack-labs/refactoring-patterns
Systematic refactoring techniques, code smell elimination, pattern extraction, and legacy modernization
npx skills add https://github.com/cosmicstack-labs/mercury-agent-skills --skill refactoring-patterns
Systematically improve code without changing its behavior.
When: A method does multiple things or is too long.
How: Find a cohesive block → extract to new method → call it.
// Before
function processOrder(order) {
const total = order.items.reduce((sum, i) => sum + i.price, 0);
const tax = total * 0.08;
const discount = order.coupon ? total * 0.1 : 0;
return total + tax - discount;
}
// After
function processOrder(order) {
const subtotal = calculateSubtotal(order);
const tax = calculateTax(subtotal);
const discount = calculateDiscount(order, subtotal);
return subtotal + tax - discount;
}
When: Switch/if-else chains based on type grow too long.
// Before
function calculateShipping(order) {
if (order.type === 'standard') return order.weight * 0.5;
if (order.type === 'express') return order.weight * 1.5 + 5;
if (order.type === 'overnight') return order.weight * 3 + 15;
}
// After
class StandardShipping {
calculate(weight) { return weight * 0.5; }
}
class ExpressShipping {
calculate(weight) { return weight * 1.5 + 5; }
}
| Refactoring | When | Risk |
|-------------|------|------|
| Rename Variable | Unclear name | Low |
| Extract Method | Long function | Low |
| Replace Magic Literal | Hard-coded values | Low |
| Introduce Parameter Object | Many parameters | Medium |
| Replace Temp with Query | Reused expressions | Low |
| Decompose Conditional | Complex condition | Medium |
| Extract Class | Class doing too much | Medium-High |
Take cosmicstack-labs/refactoring-patterns from the repository into ~/.claude/skills for personal
use, or into .claude/skills inside a project.
The agent identifies a skill by the name field in its header. Two skills with the
same name cannot sit side by side — one of them will be ignored.