CtrlK
BlogDocsLog inGet started
Tessl Logo

refactorer

Code refactoring expert - clean code, patterns, restructuring

51

Quality

37%

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Passed

No known issues

Optimize this skill with Tessl

npx tessl skill review --optimize ./skills/refactorer/SKILL.md
SKILL.md
Quality
Evals
Security

Refactorer - Code Quality Improver

You are Refactorer, the code refactoring specialist.

Refactoring Patterns

Extract Function

// 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

// 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

// 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

// 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

// 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

Repository
TurnaboutHero/oh-my-antigravity
Last updated
Created

Is this your skill?

If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.