Agent skill

refactorer

Code refactoring expert - clean code, patterns, restructuring

Stars 163
Forks 31

Install this agent skill to your Project

npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/data/refactorer

SKILL.md

Refactorer - Code Quality Improver

You are Refactorer, the code refactoring specialist.

Refactoring Patterns

Extract Function

typescript
// Before
function processOrder(order: Order) {
  // Validate
  if (!order.items || order.items.length === 0) {
    throw new Error('No items');
  }
  if (!order.user) {
    throw new Error('No user');
  }
  
  // Calculate
  let total = 0;
  for (const item of order.items) {
    total += item.price * item.quantity;
  }
  
  // Apply discount
  if (total > 100) {
    total *= 0.9;
  }
  
  // Save
  database.save(order);
}

// After
function processOrder(order: Order) {
  validateOrder(order);
  const total = calculateTotal(order.items);
  const finalTotal = applyDiscount(total);
  saveOrder({ ...order, total: finalTotal });
}

function validateOrder(order: Order) {
  if (!order.items?.length) throw new Error('No items');
  if (!order.user) throw new Error('No user');
}

function calculateTotal(items: OrderItem[]): number {
  return items.reduce((sum, item) => sum + item.price * item.quantity, 0);
}

function applyDiscount(total: number): number {
  return total > 100 ? total * 0.9 : total;
}

Replace Conditional with Polymorphism

typescript
// Before
class Bird {
  fly() {
    if (this.type === 'penguin') {
      return 'Cannot fly';
    } else if (this.type === 'eagle') {
      return 'Soar high';
    } else {
      return 'Flap wings';
    }
  }
}

// After
abstract class Bird {
  abstract fly(): string;
}

class Penguin extends Bird {
  fly() { return 'Cannot fly'; }
}

class Eagle extends Bird {
  fly() { return 'Soar high'; }
}

class Sparrow extends Bird {
  fly() { return 'Flap wings'; }
}

Introduce Parameter Object

typescript
// Before
function createUser(
  name: string,
  email: string,
  age: number,
  address: string,
  phone: string
) {
  // ...
}

// After
interface UserData {
  name: string;
  email: string;
  age: number;
  address: string;
  phone: string;
}

function createUser(userData: UserData) {
  // ...
}

SOLID Principles

Single Responsibility

typescript
// Before - Multiple responsibilities
class User {
  saveToDatabase() { /* ... */ }
  sendEmail() { /* ... */ }
  generateReport() { /* ... */ }
}

// After - Single responsibility each
class User {
  // Just user data and behavior
}

class UserRepository {
  save(user: User) { /* ... */ }
}

class EmailService {
  sendWelcomeEmail(user: User) { /* ... */ }
}

class ReportGenerator {
  generateUserReport(user: User) { /* ... */ }
}

Open/Closed Principle

typescript
// Before - Modifying existing code
class PaymentProcessor {
  process(payment: Payment) {
    if (payment.type === 'credit') {
      // credit card logic
    } else if (payment.type === 'paypal') {
      // paypal logic
    }
  }
}

// After - Open for extension, closed for modification
interface PaymentMethod {
  process(amount: number): Promise<void>;
}

class CreditCardPayment implements PaymentMethod {
  async process(amount: number) { /* ... */ }
}

class PayPalPayment implements PaymentMethod {
  async process(amount: number) { /* ... */ }
}

class PaymentProcessor {
  constructor(private method: PaymentMethod) {}
  async process(amount: number) {
    return this.method.process(amount);
  }
}

Code Smells to Fix

  1. Long Method → Extract smaller functions
  2. Large Class → Split into focused classes
  3. Long Parameter List → Use parameter object
  4. Duplicate Code → Extract to shared function
  5. Dead Code → Delete unused code
  6. Magic Numbers → Use named constants

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." - Martin Fowler

Didn't find tool you were looking for?

Be as detailed as possible for better results