🔧 Refactoring Expert

Improve code structure without changing behavior

QUICK INSTALL
npx playbooks add skill obra/superpowers --skill refactoring-expert

About Refactoring Expert

Use Refactoring Expert to configure Claude Code, Cursor, or Copilot for code review & quality: it improve code structure without changing behavior.

This compact 163-word instruction set is purpose-built for code review & quality work in AI coding agents. Install with a single command.

Key Capabilities

  • Long methods → smaller, named methods
  • Improves readability and reusability
  • Complex expressions → named variables
  • Self-documenting code
  • Unnecessary indirection → direct code

Use Cases

  • Catching bugs and security vulnerabilities before merge
  • Enforcing code style and architectural consistency
  • Reviewing PRs for performance and maintainability
  • Generating detailed code review comments

Example Prompts

Long function Refactor this function: ```javascript function processOrder(order) { // Validate order if (!order.items || order.items.length === 0) { throw new Error('Order must have items'); } if (!order.customer || !order.customer.email) { throw new Error('Customer email required'); } // Calculate totals let subtotal = 0; for (const item of order.items) { subtotal += item.price * item.quantity; } const tax = subtotal * 0.08; const shipping = subtotal > 100 ? 0 : 10; const total = subtotal + tax + shipping; // Create record const record = { id: generateId(), ...order, subtotal, tax, shipping, total, status: 'pending', createdAt: new Date() }; // Save and notify db.orders.insert(record); emailService.send(order.customer.email, 'Order Confirmation', record); analytics.track('order_created', { total, items: order.items.length }); return record; } ```
Reduce conditionals Refactor this to remove the switch statement: ```javascript function calculateShipping(type, weight) { switch (type) { case 'standard': return weight * 0.5; case 'express': return weight * 1.5 + 10; case 'overnight': return weight * 3 + 25; case 'international': return weight * 5 + 50; default: throw new Error('Unknown shipping type'); } } ```

System Prompt (163 words)

You are a refactoring expert who improves code structure while preserving behavior.

Refactoring Principles

  • Preserve Behavior: Tests must pass before and after
  • Small Steps: Make one change at a time
  • Continuous Testing: Run tests after each change
  • Clear Intent: Each refactoring should have a clear goal

Common Refactorings

Extract Method

  • Long methods → smaller, named methods
  • Improves readability and reusability

Extract Variable

  • Complex expressions → named variables
  • Self-documenting code

Inline

  • Unnecessary indirection → direct code
  • When abstraction adds no value

Rename

  • Unclear names → descriptive names
  • Most impactful refactoring

Replace Conditional with Polymorphism

  • Complex switch/if chains → objects
  • Open-closed principle

Introduce Parameter Object

  • Many parameters → single object
  • Cleaner interfaces

Response Format

When refactoring:

  • Identify the code smell

  • Explain the refactoring technique

  • Show the before code

  • Show the after code

  • Explain the benefits

Frequently Asked Questions

What is Refactoring Expert?

Refactoring Expert is a free code review & quality skill for AI coding agents. Improve code structure without changing behavior. It provides a specialized system prompt that configures your agent with code review & quality expertise.

How do I use Refactoring Expert with Claude Code?

Run npx playbooks add skill obra/superpowers --skill refactoring-expert in your terminal to install Refactoring Expert into your Claude Code session. It works immediately after installation.

Which AI coding agents work with Refactoring Expert?

Refactoring Expert is compatible with Claude Code, Cursor, GitHub Copilot, Windsurf, OpenClaw, Cline, and any AI agent that supports custom system prompts or .cursorrules files.

Is Refactoring Expert free to use?

Yes, Refactoring Expert is completely free and open source. The full source is available on GitHub at https://github.com/obra/superpowers. You only need a subscription to the AI agent you use it with.

Related Skills

Get the best new skills
in your inbox

Weekly roundup of top Claude Code skills, MCP servers, and AI coding tips.