Skip to content
Development
Skill

/refactor

Safe, incremental refactoring with SOLID evaluation. Trigger with "refactor this", "improve the code", "clean up", "restructure", "improve maintainability".

From plugin
claude-code-cookbook
1.1k39 skills9 agents39 commands8 MCP

Claude Code

$ npx -y skills add wasabeef/claude-code-cookbook --skill refactor --agent claude-code

SKILL.md

refactor.SKILL.md
description: 'Safe, incremental refactoring with SOLID evaluation. Trigger with "refactor this", "improve the code", "clean up", "restructure", "improve maintainability".'
allowed-tools:
  - Read
  - Edit
  - Grep
  - Glob
  - Bash

Safe, incremental refactoring with SOLID evaluation

Performs safe, step-by-step code refactoring with quantitative SOLID principles evaluation. Visualizes technical debt and clarifies improvement priorities.

Usage

# Identify complex code and create refactoring plan
find . -name "*.js" -exec wc -l {} + | sort -rn | head -10
"Refactor large files to reduce complexity"

# Detect and consolidate duplicate code
grep -r "function processUser" . --include="*.js"
"Consolidate duplicate functions using Extract Method"

# Evaluate SOLID principles violations
grep -r "class.*Service" . --include="*.js" | head -10
"Assess whether these classes follow Single Responsibility Principle"

Basic Examples

# Find long methods
grep -A 50 "function" src/*.js | grep -B 50 -A 50 "return" | wc -l
"Split methods over 50 lines using Extract Method"

# Find complex conditions
grep -r "if.*if.*if" . --include="*.js"
"Improve nested conditions using Strategy pattern"

# Find code smells
grep -r "TODO\|FIXME\|HACK" . --exclude-dir=node_modules
"Resolve technical debt in comments"

Refactoring Techniques

Extract Method (Split Big Functions)

// Before: Long method
function processOrder(order) {
  // 50 lines of complex processing
}

// After: Separation of responsibilities
function processOrder(order) {
  validateOrder(order);
  calculateTotal(order);
  saveOrder(order);
}

Replace Conditional with Polymorphism (Remove Switch/If Chains)

// Before: switch statement
function getPrice(user) {
  switch (user.type) {
    case "premium":
      return basePrice * 0.8;
    case "regular":
      return basePrice;
  }
}

// After: Strategy pattern
class PremiumPricing {
  calculate(basePrice) {
    return basePrice * 0.8;
  }
}

SOLID Principles Scoring (0-100 points)

Evaluation Criteria and Scoring

S - Single Responsibility (20 points)
├─ Number of responsibilities: 1 (20pts) | 2 (15pts) | 3 (10pts) | 4+ (5pts)
├─ Method count: <7 (+5pts) | 7-15 (+3pts) | >15 (0pts)
├─ Clear reasons for change: Clear (+5pts) | Unclear (0pts)
└─ Example score: UserService (auth + data processing) = 10 points

O - Open/Closed (20 points)
├─ Extension points: Strategy/Template Method (20pts) | Inheritance only (10pts) | None (5pts)
├─ Existing code changes for new features: Not needed (+5pts) | Minimal (+3pts) | Required (0pts)
├─ Interface usage: Appropriate (+5pts) | Partial (+3pts) | None (0pts)
└─ Example score: PaymentProcessor (Strategy) = 20 points

L - Liskov Substitution (20 points)
├─ Derived class contract compliance: Complete (20pts) | Partial (10pts) | Violated (0pts)
├─ Precondition strengthening: None (+5pts) | Present (-5pts)
├─ Postcondition weakening: None (+5pts) | Present (-5pts)
└─ Example score: Square extends Rectangle = 0 points (violated)

I - Interface Segregation (20 points)
├─ Interface size: 1-3 methods (20pts) | 4-7 (15pts) | 8+ (5pts)
├─ Unused method implementations: None (+5pts) | 1-2 (+2pts) | 3+ (0pts)
├─ Role clarity: Single role (+5pts) | Multiple roles (0pts)
└─ Example score: Readable/Writable separation = 20 points

D - Dependency Inversion (20 points)
├─ Dependency direction: Abstractions only (20pts) | Mixed (10pts) | Concretions only (5pts)
├─ DI usage: Constructor Injection (+5pts) | Setter (+3pts) | None (0pts)
├─ Testability: Mockable (+5pts) | Difficult (0pts)
└─ Example score: Repository Pattern = 20 points

Total Score = S + O + L + I + D
├─ 90-100 points: Excellent (SOLID compliant)
├─ 70-89 points: Good (Minor improvements needed)
├─ 50-69 points: Fair (Refactoring recommended)
├─ 30-49 points: Poor (Major improvements required)
└─ 0-29 points: Critical (Design overhaul required)

Technical Debt Quantification

Debt Calculation Formula

Technical Debt (time) = Complexity Score × Impact Range × Fix Difficulty

Complexity Score:
├─ Cyclomatic complexity: 1-5 (low) | 6-10 (med) | 11-20 (high) | 21+ (critical)
├─ Cognitive complexity: Nesting depth × conditional branches
├─ Lines of code: <50 (1pt) | 50-200 (2pts) | 200-500 (3pts) | 500+ (5pts)
└─ Duplication rate: 0-10% (1pt) | 10-30% (2pts) | 30-50% (3pts) | 50%+ (5pts)

Impact Range:
├─ Dependent modules: Direct dependencies + Indirect × 0.5
├─ Usage frequency: API calls/day
├─ Business importance: Critical (×3) | High (×2) | Medium (×1) | Low (×0.5)
└─ Team knowledge: 1 person knows (×3) | 2-3 (×2) | 4+ (×1)

Fix Difficulty:
├─ Test coverage: 0% (×3) | <50% (×2) | 50-80% (×1.5) | >80% (×1)
├─ Documentation: None (×2) | Insufficient (×1.5) | Adequate (×1)
├─ Dependencies: Tightly coupled (×3) | Moderate (×2) | Loosely coupled (×1)
└─ Change risk: Breaking change (×3) | Backward compatibility (×2) | Safe (×1)

Cost Conversion:
├─ Time cost: Debt time × Developer hourly rate
├─ Opportunity cost: New feature delay days × Daily revenue impact
├─ Quality cost: Bug probability × Fix cost × Frequency
└─ Total cost: Time + Opportunity + Quality costs

Priority Matrix

| Priority | Impact | Fix Cost | Time Savings | Investment ROI | Response Deadline | | -------------------------- | ------ | -------- | ------------ | --------------------- | ----------------- | | **Critical (Immediate)** | High | Low | > 5x | Invest 1h → Save 5h+ | Immediately | | **Important (Planned)** | High | High | 2-5x | Invest 1h → Save 2-5h | Within 1 month | | **Watch (Monitor)** | Low | High | 1-2x | Invest 1h → Save 1-2h | Within 3 months | | **Acceptable (Tolerable)** | Low | Low | < 1x | Investment = Savings | No action needed |

Refactoring Process

1. **Current Analysis and Measurement**

  • Measure c
Read more
@wasabeef
Ships withclaude-code-cookbook

A collection of commands, roles, and automation scripts for Claude Code. Automate your workflow without unnecessary confirmations, allowing you to focus on what matters.

1.1k39 skills9 agents8 MCP
Get the whole plugin

Stars on GitHub

Momentum

stars in total
1.1k
Star history of wasabeef/claude-code-cookbook on GitHub, a chart by star-history.com

Other skills on claude-code-cookbook.

check-fact

check-fact

Verify information accuracy against codebase and documentation. Trigger with "is this correct?", "fact check", "verify this", "is…

More skills from other plugins.

brainstorming
Auto-invoked

brainstorming

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or…

@obra@obraInstall Skill
ask-matt

ask-matt

Ask which skill or flow fits your situation. A router over the skills in this repo.

skills13d309k
@mattpocock@mattpocockInstall Skill
accessibility
Auto-invoked

accessibility

Design, implement, and audit accessible UI to WCAG 2.2 Level AA across Web, iOS, and Android — semantic ARIA roles and labels,…

ecc2w293k
@affaan-m@affaan-mInstall Skill
compiler-commit

compiler-commit

Use when you want to verify compiler changes and commit with the correct convention. Runs tests, lint, and format, then commits…

react4d252k
@facebook@facebookInstall Skill

Questions about refactor.

Which coding agents can use refactor?
refactor installs in Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, OpenCode and Windsurf with the skills CLI, using each agent's flag shown on this page. Flowy's auto-invocation, which fires a skill when your prompt matches, runs in Claude Code only.