refactoring
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRefactoring
代码重构
Quick Start
快速开始
- Define scope and boundaries
- Cover with tests
- Refactor in small steps (one technique per commit)
- Verify after each change
- 定义范围与边界
- 编写测试覆盖
- 分步实施重构(每次提交使用一种重构技巧)
- 每次变更后进行验证
Workflow
工作流程
Before Refactoring
重构前准备
- Define scope, find boundaries (junction points between code to change and everything else)
- Cover with tests (edge cases, explicit/implicit input, specify desired result on boundaries)
- Make linter/compiler stricter
See references/01-preparation.md
- 定义范围,明确边界(待修改代码与其他代码的衔接点)
- 编写测试覆盖(包括边界情况、显式/隐式输入,明确边界处的预期结果)
- 启用更严格的代码检查器/编译器规则
参考references/01-preparation.md
Planning a Refactor
重构规划
- Gather detailed problem description
- Explore codebase to verify assertions
- Consider options and alternatives
- Interview user about implementation details
- Define exact scope (in/out)
- Check test coverage
- Break into tiny commits
See references/02-planning-workflow.md
- 收集详细的问题描述
- 调研代码库以验证假设
- 评估多种可选方案
- 与用户沟通实现细节
- 明确精确的范围(包含/排除内容)
- 检查测试覆盖率
- 拆分为多个小型提交
参考references/02-planning-workflow.md
During Refactoring
重构实施中
- Small atomic steps
- One technique per commit
- No features or bug fixes during refactor
- Test every change
- Refactor tests separately from production code
See references/03-process-heuristics.md
- 采用原子化的小步骤
- 每次提交仅使用一种重构技巧
- 重构期间不添加新功能或修复Bug
- 对每次变更进行测试
- 单独重构测试代码,与生产代码分离
参考references/03-process-heuristics.md
Techniques Index
重构技术索引
| Level | File | Techniques |
|---|---|---|
| Setup | 04-low-hanging-fruit.md | Formatting, linting, language/API features |
| Code | 05-names.md | Naming problems & solutions |
| Code | 06-duplication.md | DRY, extraction techniques |
| Code | 07-conditions.md | Guard clauses, complexity reduction |
| Code | 08-functional-pipeline.md | Composition, data flow |
| Design | 09-abstraction.md | Abstraction levels, leak prevention |
| Design | 10-side-effects.md | Isolation, CQS |
| Design | 11-error-handling.md | Error types, strategies |
| Design | 12-generics.md | Generics, composition |
| Architecture | 13-module-integration.md | Coupling, cohesion |
| Architecture | 14-architecture.md | Layers, dependencies |
| Architecture | 15-declarative-style.md | Declarative patterns |
| Architecture | 16-static-typing.md | Type design |
| Support | 17-test-code.md | Refactoring tests |
| Support | 18-comments-docs.md | Documentation |
| 层级 | 文件 | 技术内容 |
|---|---|---|
| 基础准备 | 04-low-hanging-fruit.md | 代码格式化、代码检查、语言/API特性运用 |
| 代码层面 | 05-names.md | 命名问题与解决方案 |
| 代码层面 | 06-duplication.md | DRY原则、代码提取技巧 |
| 代码层面 | 07-conditions.md | 卫语句、复杂度降低 |
| 代码层面 | 08-functional-pipeline.md | 函数组合、数据流管理 |
| 设计层面 | 09-abstraction.md | 抽象层级、避免抽象泄漏 |
| 设计层面 | 10-side-effects.md | 副作用隔离、命令查询分离(CQS) |
| 设计层面 | 11-error-handling.md | 错误类型、处理策略 |
| 设计层面 | 12-generics.md | 泛型运用、组合模式 |
| 架构层面 | 13-module-integration.md | 耦合度、内聚性 |
| 架构层面 | 14-architecture.md | 分层架构、依赖管理 |
| 架构层面 | 15-declarative-style.md | 声明式编程模式 |
| 架构层面 | 16-static-typing.md | 类型设计 |
| 支持层面 | 17-test-code.md | 测试代码重构 |
| 支持层面 | 18-comments-docs.md | 文档编写 |
Principles
原则
- Small steps: Each commit leaves code in working state (Fowler: "Make each refactoring step as small as possible, so that you can always see the program working")
- One technique at a time: Don't mix refactoring techniques
- No scope creep: No features or bug fixes during refactor
- Test-driven: Verify after every change
- 分步实施:每次提交后代码需保持可运行状态(Fowler:"让每个重构步骤尽可能小,这样你总能看到程序正常运行")
- 单一技巧:不要混合使用多种重构技巧
- 范围可控:重构期间不添加新功能或修复Bug
- 测试驱动:每次变更后进行验证
Templates
模板
See references/19-plan-template.md for refactor plan issue/PR template.
重构计划的Issue/PR模板请参考references/19-plan-template.md
Quick Reference
速查手册
See references/20-cheatsheet.md for technique summary.
重构技术汇总速查请参考references/20-cheatsheet.md