refactoring-advisor
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRefactoring Advisor
重构顾问
Quick Start
快速开始
Plan refactoring based on identified issues:
- Identify the problem (code smell, complexity)
- Select appropriate refactoring pattern
- Create step-by-step plan
- Ensure tests exist
- Refactor incrementally
基于已发现的问题规划重构:
- 识别问题(代码坏味、复杂度问题)
- 选择合适的重构模式
- 创建分步计划
- 确保测试存在
- 增量式重构
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:选择重构策略
| Problem | Strategy | Risk |
|---|---|---|
| Long Method | Extract Method | Low |
| God Class | Extract Class | Medium |
| Duplicated Code | Extract Method | Low |
| Switch Statement | Replace with Polymorphism | High |
| Long Parameter List | Introduce Parameter Object | Medium |
| 问题 | 策略 | 风险 |
|---|---|---|
| 长方法 | 提取方法 | 低 |
| 上帝类 | 提取类 | 中 |
| 重复代码 | 提取方法 | 低 |
| 分支语句 | 替换为多态 | 高 |
| 长参数列表 | 引入参数对象 | 中 |
Step 3: Create Refactoring Plan
步骤3:创建重构计划
Template:
markdown
undefined模板:
markdown
undefinedRefactoring: [Name]
重构:[名称]
Problem: [Description]
Goal: [Desired outcome]
Risk Level: [Low/Medium/High]
问题: [描述]
目标: [预期结果]
风险等级: [低/中/高]
Prerequisites
前置条件
- Tests exist and pass
- Code is committed
- Dependencies identified
- 测试已存在且全部通过
- 代码已提交至版本控制
- 已识别所有依赖项
Steps
步骤
- [First small change]
- [Run tests]
- [Next small change]
- [Run tests] ...
- [第一个小变更]
- [运行测试]
- [下一个小变更]
- [运行测试] ...
Validation
验证
- All tests pass
- No functionality changed
- Code is cleaner
undefined- 所有测试通过
- 未改变原有功能
- 代码结构更清晰
undefinedStep 4: Execute Incrementally
步骤4:增量式执行
Golden Rule: Make the change easy, then make the easy change
- Make one small change
- Run tests
- Commit
- Repeat
黄金法则: 先让变更变得容易,再完成这个简易变更
- 做出一个小变更
- 运行测试
- 提交代码
- 重复上述步骤
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:
- Extract banner printing
- Run tests
- Extract outstanding calculation
- Run tests
- Extract details printing
- 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}`);
}计划:
- 提取横幅打印逻辑
- 运行测试
- 提取应付款计算逻辑
- 运行测试
- 提取详情打印逻辑
- 运行测试
重构后:
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:
- Create TelephoneNumber class
- Move areaCode field
- Run tests
- Move number field
- Run tests
- Move getTelephoneNumber method
- Run tests
- Update Person to use TelephoneNumber
- 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}`;
}
}计划:
- 创建TelephoneNumber类
- 迁移areaCode字段
- 运行测试
- 迁移number字段
- 运行测试
- 迁移getTelephoneNumber方法
- 运行测试
- 更新Person类以使用TelephoneNumber
- 运行测试
重构后:
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
重构前:
- 测试已存在且全部通过
- 代码已纳入版本控制
- 你理解代码的功能
- 你有足够时间完成重构
重构中:
- 做出小幅度变更
- 每次变更后运行测试
- 提交可正常运行的代码版本
- 重构时不要添加新功能
重构后:
- 所有测试通过
- 已完成代码评审
- 文档已更新
- 原有功能未发生改变