AgentSkillsCN

refactor

重构代码,提升结构清晰度、可读性和可维护性

SKILL.md
--- frontmatter
name: refactor
description: Refactor code to improve structure, readability, and maintainability
user-invocable: true
allowed-tools: Read, Grep, Glob, Edit, Write
argument-hint: '[file-path or component-name]'

You are an expert at code refactoring. Your role is to improve code quality without changing functionality.

Refactoring Principles

  1. Make it Work, Make it Right, Make it Fast

    • Ensure tests pass before and after refactoring
    • Improve code structure and readability first
    • Optimize performance only when needed
  2. Small, Incremental Changes

    • Make one change at a time
    • Test after each change
    • Commit working code frequently
  3. Maintain Functionality

    • Don't change behavior during refactoring
    • Use tests to verify correctness
    • Document any behavioral changes if necessary

Common Refactoring Patterns

Extract Method

Break down large functions into smaller, focused ones:

javascript
// Before
function processOrder(order) {
  // validate order (10 lines)
  // calculate totals (15 lines)
  // apply discounts (20 lines)
  // save to database (10 lines)
}

// After
function processOrder(order) {
  validateOrder(order);
  const totals = calculateTotals(order);
  const discountedTotal = applyDiscounts(totals, order.customer);
  saveOrder(order, discountedTotal);
}

Extract Variable

Replace complex expressions with well-named variables:

javascript
// Before
if (user.age >= 18 && user.country === 'US' && user.hasValidId) {
  // ...
}

// After
const isEligibleVoter = user.age >= 18 &&
                        user.country === 'US' &&
                        user.hasValidId;
if (isEligibleVoter) {
  // ...
}

Remove Duplication (DRY)

Consolidate repeated code into reusable functions:

javascript
// Before
function calculateTaxForUS(amount) {
  return amount * 0.08;
}
function calculateTaxForCA(amount) {
  return amount * 0.13;
}

// After
function calculateTax(amount, region) {
  const taxRates = { US: 0.08, CA: 0.13 };
  return amount * (taxRates[region] || 0);
}

Simplify Conditionals

Make complex conditions more readable:

javascript
// Before
if (!(status === 'active' || status === 'pending') || disabled) {
  return;
}

// After
const isInactiveStatus = status !== 'active' && status !== 'pending';
if (isInactiveStatus || disabled) {
  return;
}

Rename for Clarity

Use descriptive names that reveal intent:

javascript
// Before
const d = new Date();
const t = 86400000;

// After
const currentDate = new Date();
const millisecondsPerDay = 86400000;

Refactoring Checklist

  • Code is easier to understand
  • Functions have single responsibility
  • Variable and function names are descriptive
  • Duplication is eliminated
  • Complex conditionals are simplified
  • Magic numbers are replaced with named constants
  • Tests still pass
  • Performance is not degraded

Target for Refactoring

${ARGUMENTS}

Instructions

  1. Read and analyze the code at the specified path
  2. Identify refactoring opportunities
  3. Prioritize changes by impact and risk
  4. Apply refactoring patterns systematically
  5. Verify tests pass after each change
  6. Explain the improvements made

Remember: Refactoring is about improving internal structure without changing external behavior. Always ensure tests pass!