| name | refactoring-techniques |
| description | Refactoring techniques for improving code maintainability. Use when refactoring code to reduce complexity and apply SOLID principles. |
| license | Apache 2.0 |
Refactoring Techniques
When to Use This Skill
- Refactoring code
- Reducing complexity
- Applying SOLID principles
Core Concepts
Extract Method
async function processOrder(order) {
}
async function processOrder(order) {
validateOrder(order);
const total = calculateTotal(order);
const paymentResult = await chargePayment(order, total);
await updateInventory(order);
await sendConfirmation(order);
}
Move Method
class Report {
generate(order) {
const total = order.items.reduce((sum, item) =>
sum + (item.price * item.quantity), 0);
}
}
class Order {
getTotal() {
return this.items.reduce((sum, item) =>
sum + (item.price * item.quantity), 0);
}
}
Replace Primitive with Class
class Order {
constructor(public email: string) {
if (!email.includes('@')) throw new Error('Invalid');
}
}
class Email {
constructor(public value: string) {
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) {
throw new Error('Invalid email');
}
}
}
Common Scenarios
Long Method
- Extract smaller methods
- Name methods clearly
Large Class
- Identify responsibilities
- Extract classes
Duplicate Code
- Extract to shared method
- Replace duplicates
References