From charon-fan-agent-playbook
Refactors code to improve structure, readability, and maintainability by addressing smells like long methods, duplicates, large classes, long parameter lists, and feature envy. Use for cleanup or quality improvement requests.
npx claudepluginhub zhaono1/agent-playbookThis skill is limited to using the following tools:
Expert guidance on refactoring code to improve structure, readability, and maintainability while preserving functionality.
Refactors code surgically to improve maintainability without changing behavior: extracts functions, renames variables, breaks god functions, eliminates smells, boosts type safety, applies patterns. For gradual improvements on hard-to-maintain code.
Surgical refactoring improves code maintainability without behavior changes: extracts functions, renames variables, breaks god functions, boosts type safety, removes smells.
Guides safe, incremental refactoring to improve code quality without changing behavior. Generates analysis/plans via refactor:analyze/plan commands, covers smells and patterns like Extract Method.
Share bugs, ideas, or general feedback.
Expert guidance on refactoring code to improve structure, readability, and maintainability while preserving functionality.
Activates when you:
Symptom: Function > 20-30 lines
Refactoring: Extract Method
// Before:
function processOrder(order) {
// 50 lines of code
}
// After:
function processOrder(order) {
validateOrder(order);
calculateTotals(order);
saveOrder(order);
sendConfirmation(order);
}
Symptom: Similar code in multiple places
Refactoring: Extract Method / Template Method
// Before:
class UserService {
async validateEmail(email) {
if (!email || !email.includes('@')) return false;
const domain = email.split('@')[1];
return domain.length > 0;
}
}
class AdminService {
async validateEmail(email) {
if (!email || !email.includes('@')) return false;
const domain = email.split('@')[1];
return domain.length > 0;
}
}
// After:
class EmailValidator {
async validate(email) {
if (!email || !email.includes('@')) return false;
return email.split('@')[1].length > 0;
}
}
Symptom: Class doing too many things
Refactoring: Extract Class
// Before:
class User {
// Authentication
// Profile management
// Notifications
// Reporting
}
// After:
class User { /* Core user data */ }
class UserAuth { /* Authentication */ }
class UserProfile { /* Profile management */ }
class UserNotifier { /* Notifications */ }
Symptom: Function with 4+ parameters
Refactoring: Introduce Parameter Object
// Before:
function createUser(name, email, age, address, phone, role) { ... }
// After:
function createUser(user: UserData) { ... }
interface UserData {
name: string;
email: string;
age: number;
address: string;
phone: string;
role: string;
}
Symptom: Method uses more data from other classes
Refactoring: Move Method
// Before:
class Report {
formatSummary(formatter) {
const options = formatter.getFormattingOptions();
// ...
}
}
// After:
class Formatter {
formatReport(report) {
const discount = this.discountLevel;
// ...
}
}
Symptom: Same data appearing together
Refactoring: Extract Value Object
// Before:
function drawShape(x, y, width, height) { ... }
function moveShape(x, y, width, height, dx, dy) { ... }
// After:
class Rectangle {
constructor(x, y, width, height) { ... }
}
function drawShape(rect: Rectangle) { ... }
Symptom: Using primitives instead of small objects
Refactoring: Replace Primitive with Object
// Before:
function createUser(name, email, phone) { ... }
// After:
class Email {
constructor(value) {
if (!this.isValid(value)) throw new Error('Invalid email');
this.value = value;
}
// ...
}
Symptom: Large switch on type
Refactoring: Replace Conditional with Polymorphism
// Before:
function calculatePay(employee) {
switch (employee.type) {
case 'engineer': return employee.salary * 1.2;
case 'manager': return employee.salary * 1.5;
case 'sales': return employee.salary * 1.1;
}
}
// After:
interface Employee {
calculatePay(): number;
}
class Engineer implements Employee {
calculatePay() { return this.salary * 1.2; }
}
Symptom: Variables only used in certain scenarios
Refactoring: Extract Class
// Before:
class User {
calculateRefund() {
this.tempRefundAmount = 0;
// complex calculation
return this.tempRefundAmount;
}
}
// After:
class RefundCalculator {
calculate(user) {
// ...
}
}
Symptom: Code needs extensive comments
Refactoring: Extract Method with clear name
// Before:
// Calculate the total price including discounts
// and tax based on user location
function calc(u, i) {
let t = 0;
// discount logic
if (u.vip) t *= 0.9;
// tax logic
if (u.state === 'CA') t *= 1.08;
return t;
}
// After:
function calculateTotalPrice(user: User, items: Item[]): number {
let total = items.sum(i => i.price);
if (user.isVIP) {
total = applyVIPDiscount(total);
}
return applyTax(total, user.state);
}
references/smells.md - Complete code smell catalogreferences/techniques.md - Refactoring techniquesreferences/checklist.md - Refactoring checklist