refactoring

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Refactoring

代码重构

Quick Start

快速开始

  1. Define scope and boundaries
  2. Cover with tests
  3. Refactor in small steps (one technique per commit)
  4. Verify after each change
  1. 定义范围与边界
  2. 编写测试覆盖
  3. 分步实施重构(每次提交使用一种重构技巧)
  4. 每次变更后进行验证

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

重构技术索引

LevelFileTechniques
Setup04-low-hanging-fruit.mdFormatting, linting, language/API features
Code05-names.mdNaming problems & solutions
Code06-duplication.mdDRY, extraction techniques
Code07-conditions.mdGuard clauses, complexity reduction
Code08-functional-pipeline.mdComposition, data flow
Design09-abstraction.mdAbstraction levels, leak prevention
Design10-side-effects.mdIsolation, CQS
Design11-error-handling.mdError types, strategies
Design12-generics.mdGenerics, composition
Architecture13-module-integration.mdCoupling, cohesion
Architecture14-architecture.mdLayers, dependencies
Architecture15-declarative-style.mdDeclarative patterns
Architecture16-static-typing.mdType design
Support17-test-code.mdRefactoring tests
Support18-comments-docs.mdDocumentation
层级文件技术内容
基础准备04-low-hanging-fruit.md代码格式化、代码检查、语言/API特性运用
代码层面05-names.md命名问题与解决方案
代码层面06-duplication.mdDRY原则、代码提取技巧
代码层面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