AgentSkillsCN

refactorer

运用系统化的重构模式来提升代码质量。每次仅执行一步安全且经过验证的重构操作。当用户说“重构”“清理代码”“提取方法”“重命名”或“优化代码”时,可使用此技能。

SKILL.md
--- frontmatter
name: refactorer
description: "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'."
allowed-tools: Read, Write, Edit, Glob, Grep

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

RefactoringWhen to Use
Extract FunctionLong function, reusable logic
Extract ClassClass doing too much
RenameUnclear naming
InlineOver-abstracted code
MoveWrong location
Replace Conditional with PolymorphismComplex type switching
Extract VariableComplex expression
Introduce Parameter ObjectMany 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

SmellSymptomsRefactoring
Long Method>20 linesExtract Function
Large Class>200 linesExtract Class
Duplicate CodeSame code in 2+ placesExtract Function
Long Parameter List>3 parametersParameter Object
Feature EnvyMethod uses other class moreMove Method
Data ClumpsSame fields togetherExtract 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