refactoring-advisor

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Refactoring Advisor

重构顾问

Quick Start

快速开始

Plan refactoring based on identified issues:
  1. Identify the problem (code smell, complexity)
  2. Select appropriate refactoring pattern
  3. Create step-by-step plan
  4. Ensure tests exist
  5. Refactor incrementally
基于已发现的问题规划重构:
  1. 识别问题(代码坏味、复杂度问题)
  2. 选择合适的重构模式
  3. 创建分步计划
  4. 确保测试存在
  5. 增量式重构

Instructions

操作指南

Step 1: Assess Current State

步骤1:评估当前状态

Analyze the code:
  • What is the problem? (long method, god class, etc.)
  • What is the impact? (maintainability, testability)
  • What is the risk? (how much code depends on it)
分析代码:
  • 存在什么问题?(长方法、上帝类等)
  • 影响是什么?(可维护性、可测试性)
  • 风险是什么?(有多少代码依赖它)

Step 2: Select Refactoring Strategy

步骤2:选择重构策略

ProblemStrategyRisk
Long MethodExtract MethodLow
God ClassExtract ClassMedium
Duplicated CodeExtract MethodLow
Switch StatementReplace with PolymorphismHigh
Long Parameter ListIntroduce Parameter ObjectMedium
问题策略风险
长方法提取方法
上帝类提取类
重复代码提取方法
分支语句替换为多态
长参数列表引入参数对象

Step 3: Create Refactoring Plan

步骤3:创建重构计划

Template:
markdown
undefined
模板:
markdown
undefined

Refactoring: [Name]

重构:[名称]

Problem: [Description] Goal: [Desired outcome] Risk Level: [Low/Medium/High]
问题: [描述] 目标: [预期结果] 风险等级: [低/中/高]

Prerequisites

前置条件

  • Tests exist and pass
  • Code is committed
  • Dependencies identified
  • 测试已存在且全部通过
  • 代码已提交至版本控制
  • 已识别所有依赖项

Steps

步骤

  1. [First small change]
  2. [Run tests]
  3. [Next small change]
  4. [Run tests] ...
  1. [第一个小变更]
  2. [运行测试]
  3. [下一个小变更]
  4. [运行测试] ...

Validation

验证

  • All tests pass
  • No functionality changed
  • Code is cleaner
undefined
  • 所有测试通过
  • 未改变原有功能
  • 代码结构更清晰
undefined

Step 4: Execute Incrementally

步骤4:增量式执行

Golden Rule: Make the change easy, then make the easy change
  1. Make one small change
  2. Run tests
  3. Commit
  4. Repeat
黄金法则: 先让变更变得容易,再完成这个简易变更
  1. 做出一个小变更
  2. 运行测试
  3. 提交代码
  4. 重复上述步骤

Refactoring Examples

重构示例

Extract Method

提取方法

Before:
javascript
function printOwing(invoice) {
  let outstanding = 0;
  
  console.log("***********************");
  console.log("**** Customer Owes ****");
  console.log("***********************");
  
  for (const o of invoice.orders) {
    outstanding += o.amount;
  }
  
  console.log(`name: ${invoice.customer}`);
  console.log(`amount: ${outstanding}`);
}
Plan:
  1. Extract banner printing
  2. Run tests
  3. Extract outstanding calculation
  4. Run tests
  5. Extract details printing
  6. Run tests
After:
javascript
function printOwing(invoice) {
  printBanner();
  const outstanding = calculateOutstanding(invoice);
  printDetails(invoice, outstanding);
}

function printBanner() {
  console.log("***********************");
  console.log("**** Customer Owes ****");
  console.log("***********************");
}

function calculateOutstanding(invoice) {
  return invoice.orders.reduce((sum, o) => sum + o.amount, 0);
}

function printDetails(invoice, outstanding) {
  console.log(`name: ${invoice.customer}`);
  console.log(`amount: ${outstanding}`);
}
重构前:
javascript
function printOwing(invoice) {
  let outstanding = 0;
  
  console.log("***********************");
  console.log("**** Customer Owes ****");
  console.log("***********************");
  
  for (const o of invoice.orders) {
    outstanding += o.amount;
  }
  
  console.log(`name: ${invoice.customer}`);
  console.log(`amount: ${outstanding}`);
}
计划:
  1. 提取横幅打印逻辑
  2. 运行测试
  3. 提取应付款计算逻辑
  4. 运行测试
  5. 提取详情打印逻辑
  6. 运行测试
重构后:
javascript
function printOwing(invoice) {
  printBanner();
  const outstanding = calculateOutstanding(invoice);
  printDetails(invoice, outstanding);
}

function printBanner() {
  console.log("***********************");
  console.log("**** Customer Owes ****");
  console.log("***********************");
}

function calculateOutstanding(invoice) {
  return invoice.orders.reduce((sum, o) => sum + o.amount, 0);
}

function printDetails(invoice, outstanding) {
  console.log(`name: ${invoice.customer}`);
  console.log(`amount: ${outstanding}`);
}

Extract Class

提取类

Before:
javascript
class Person {
  name;
  officeAreaCode;
  officeNumber;
  
  getTelephoneNumber() {
    return `(${this.officeAreaCode}) ${this.officeNumber}`;
  }
}
Plan:
  1. Create TelephoneNumber class
  2. Move areaCode field
  3. Run tests
  4. Move number field
  5. Run tests
  6. Move getTelephoneNumber method
  7. Run tests
  8. Update Person to use TelephoneNumber
  9. Run tests
After:
javascript
class TelephoneNumber {
  areaCode;
  number;
  
  toString() {
    return `(${this.areaCode}) ${this.number}`;
  }
}

class Person {
  name;
  telephoneNumber;
  
  getTelephoneNumber() {
    return this.telephoneNumber.toString();
  }
}
重构前:
javascript
class Person {
  name;
  officeAreaCode;
  officeNumber;
  
  getTelephoneNumber() {
    return `(${this.officeAreaCode}) ${this.officeNumber}`;
  }
}
计划:
  1. 创建TelephoneNumber类
  2. 迁移areaCode字段
  3. 运行测试
  4. 迁移number字段
  5. 运行测试
  6. 迁移getTelephoneNumber方法
  7. 运行测试
  8. 更新Person类以使用TelephoneNumber
  9. 运行测试
重构后:
javascript
class TelephoneNumber {
  areaCode;
  number;
  
  toString() {
    return `(${this.areaCode}) ${this.number}`;
  }
}

class Person {
  name;
  telephoneNumber;
  
  getTelephoneNumber() {
    return this.telephoneNumber.toString();
  }
}

Safety Checklist

安全检查清单

Before refactoring:
  • Tests exist and pass
  • Code is in version control
  • You understand what the code does
  • You have time to complete the refactoring
During refactoring:
  • Make small changes
  • Run tests after each change
  • Commit working states
  • Don't add features while refactoring
After refactoring:
  • All tests pass
  • Code review completed
  • Documentation updated
  • No functionality changed
重构前:
  • 测试已存在且全部通过
  • 代码已纳入版本控制
  • 你理解代码的功能
  • 你有足够时间完成重构
重构中:
  • 做出小幅度变更
  • 每次变更后运行测试
  • 提交可正常运行的代码版本
  • 重构时不要添加新功能
重构后:
  • 所有测试通过
  • 已完成代码评审
  • 文档已更新
  • 原有功能未发生改变