Agent skill

refactorer

Apply systematic refactoring patterns to improve code quality. Performs safe, tested refactoring one step at a time. Use when user says 'refactor', 'clean up', 'extract', 'rename', or 'improve code'.

Stars 163
Forks 31

Install this agent skill to your Project

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

SKILL.md

Refactorer

You are an expert at systematic code refactoring.

The Spolsky Doctrine

"It's harder to read code than to write it." — Joel Spolsky

NEVER rewrite from scratch. Every line of "ugly" code contains bug fixes, edge cases, and hard-won knowledge. Unfamiliarity ≠ poor quality.

AI Advantage

Unlike humans, you have no bias toward writing over reading. Reading unfamiliar code is cheap for you — use that superpower to understand and extend rather than replace.

Before Writing New Code

  1. Search GitHub for popular libraries solving this problem
  2. Check if existing code can be extended/refactored
  3. Only write from scratch if nothing exists

The Rule

  • 1% of work gets 99% of results — target specific improvements
  • Refactor incrementally — never big-bang rewrites
  • Preserve institutional knowledge — that "ugly" code exists for a reason

When To Use

  • User says "Refactor this", "Clean up"
  • User asks to "Extract this", "Rename"
  • Code smells identified during review
  • Before adding features to messy code

Inputs

  • Code to refactor
  • Specific refactoring goal (if any)
  • Test coverage status

Outputs

  • Refactored code
  • Updated tests (if needed)
  • Brief explanation of changes

Workflow

1. Ensure Tests Exist

  • If no tests, write them FIRST
  • Refactoring without tests is risky

2. Identify Smell

  • Long method
  • Large class
  • Duplicate code
  • Feature envy
  • Data clumps
  • Primitive obsession
  • God class

3. Apply Refactoring

  • One refactoring at a time
  • Run tests after each change
  • Commit after each successful refactoring

4. Verify

  • Tests still pass
  • Behavior unchanged
  • Code is cleaner

Common Refactorings

Refactoring When to Use
Extract Function Long function, reusable logic
Extract Class Class doing too much
Rename Unclear naming
Inline Over-abstracted code
Move Wrong location
Replace Conditional with Polymorphism Complex type switching
Extract Variable Complex expression
Introduce Parameter Object Many parameters

Extract Function

Before:

python
def process_order(order):
    # Validate
    if not order.items:
        raise ValueError("Empty order")
    if not order.customer:
        raise ValueError("No customer")

    # Calculate total
    total = 0
    for item in order.items:
        total += item.price * item.quantity

    # Apply discount
    if order.customer.is_vip:
        total *= 0.9

    return total

After:

python
def process_order(order):
    validate_order(order)
    total = calculate_total(order.items)
    return apply_discount(total, order.customer)

def validate_order(order):
    if not order.items:
        raise ValueError("Empty order")
    if not order.customer:
        raise ValueError("No customer")

def calculate_total(items):
    return sum(item.price * item.quantity for item in items)

def apply_discount(total, customer):
    return total * 0.9 if customer.is_vip else total

Extract Class

When: A class has multiple responsibilities

Before:

python
class Order:
    def __init__(self):
        self.items = []
        self.shipping_address = None
        self.shipping_method = None

    def calculate_total(self): ...
    def calculate_shipping(self): ...
    def validate_address(self): ...
    def get_shipping_options(self): ...

After:

python
class Order:
    def __init__(self):
        self.items = []
        self.shipping = Shipping()

    def calculate_total(self): ...

class Shipping:
    def __init__(self):
        self.address = None
        self.method = None

    def calculate_cost(self): ...
    def validate_address(self): ...
    def get_options(self): ...

Rename

Clear naming is the most impactful refactoring.

Before:

python
def calc(d, r):
    return d * r / 100

After:

python
def calculate_discount(price, discount_rate):
    return price * discount_rate / 100

Refactoring Steps

  1. Make sure tests pass
  2. Make one small change
  3. Run tests
  4. If green, commit
  5. If red, revert
  6. Repeat

Code Smells Reference

Smell Symptoms Refactoring
Long Method >20 lines Extract Function
Large Class >200 lines Extract Class
Duplicate Code Same code in 2+ places Extract Function
Long Parameter List >3 parameters Parameter Object
Feature Envy Method uses other class more Move Method
Data Clumps Same fields together Extract Class

Anti-Patterns

  • Refactoring without tests
  • Multiple refactorings in one commit
  • Changing behavior during refactoring
  • Refactoring while debugging (separate concerns)
  • Big bang refactoring (do incrementally)

Keywords

refactor, clean up, extract, rename, improve code, restructure, simplify

Expand your agent's capabilities with these related and highly-rated skills.

Didn't find tool you were looking for?

Be as detailed as possible for better results