refactor
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFowler/Martin Code Refactoring Best Practices
Fowler/Martin代码重构最佳实践
Comprehensive code refactoring guide based on Martin Fowler's catalog and Clean Code principles, designed for AI agents and LLMs. Contains 43 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
基于Martin Fowler的重构目录与Clean Code原则打造的综合代码重构指南,专为AI Agent与LLM设计。包含8个类别共43条规则,按影响优先级排序,可指导自动化重构与代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Refactoring existing code to improve maintainability
- Decomposing long methods or large classes
- Reducing coupling between components
- Simplifying complex conditional logic
- Reviewing code for code smells and anti-patterns
在以下场景中可参考本指南:
- 重构现有代码以提升可维护性
- 拆分过长方法或大型类
- 降低组件间的耦合度
- 简化复杂的条件逻辑
- 评审代码以识别代码异味与反模式
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Structure & Decomposition | CRITICAL | |
| 2 | Coupling & Dependencies | CRITICAL | |
| 3 | Naming & Clarity | HIGH | |
| 4 | Conditional Logic | HIGH | |
| 5 | Abstraction & Patterns | MEDIUM-HIGH | |
| 6 | Data Organization | MEDIUM | |
| 7 | Error Handling | MEDIUM | |
| 8 | Micro-Refactoring | LOW | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 结构与分解 | 关键 | |
| 2 | 耦合与依赖 | 关键 | |
| 3 | 命名与清晰度 | 高 | |
| 4 | 条件逻辑 | 高 | |
| 5 | 抽象与模式 | 中高 | |
| 6 | 数据组织 | 中 | |
| 7 | 错误处理 | 中 | |
| 8 | 微重构 | 低 | |
Quick Reference
快速参考
1. Structure & Decomposition (CRITICAL)
1. 结构与分解(关键)
- - Extract Method for Long Functions
struct-extract-method - - Apply Single Responsibility Principle
struct-single-responsibility - - Extract Class from Large Class
struct-extract-class - - Compose Method for Readable Flow
struct-compose-method - - Keep Functions Under 20 Lines
struct-function-length - - Replace Method with Method Object
struct-replace-method-with-object - - Introduce Parameter Object
struct-parameter-object
- - 为长函数提取方法
struct-extract-method - - 应用单一职责原则
struct-single-responsibility - - 从大型类中提取类
struct-extract-class - - 组合方法以实现可读流程
struct-compose-method - - 函数代码行数控制在20行以内
struct-function-length - - 用方法对象替代方法
struct-replace-method-with-object - - 引入参数对象
struct-parameter-object
2. Coupling & Dependencies (CRITICAL)
2. 耦合与依赖(关键)
- - Use Dependency Injection
couple-dependency-injection - - Hide Delegate to Reduce Coupling
couple-hide-delegate - - Remove Middle Man When Excessive
couple-remove-middle-man - - Fix Feature Envy by Moving Methods
couple-feature-envy - - Apply Interface Segregation Principle
couple-interface-segregation - - Preserve Whole Object Instead of Fields
couple-preserve-whole-object
- - 使用依赖注入
couple-dependency-injection - - 隐藏委托以降低耦合
couple-hide-delegate - - 过度时移除中间层
couple-remove-middle-man - - 通过移动方法修复特性羡慕问题
couple-feature-envy - - 应用接口隔离原则
couple-interface-segregation - - 保留完整对象而非字段
couple-preserve-whole-object
3. Naming & Clarity (HIGH)
3. 命名与清晰度(高)
- - Use Intention-Revealing Names
name-intention-revealing - - Avoid Abbreviations and Acronyms
name-avoid-abbreviations - - Use Consistent Vocabulary
name-consistent-vocabulary - - Use Searchable Names
name-searchable-names - - Avoid Type Encodings in Names
name-avoid-encodings
- - 使用能表达意图的命名
name-intention-revealing - - 避免缩写与首字母缩写词
name-avoid-abbreviations - - 使用一致的术语词汇
name-consistent-vocabulary - - 使用便于搜索的命名
name-searchable-names - - 避免在命名中使用类型编码
name-avoid-encodings
4. Conditional Logic (HIGH)
4. 条件逻辑(高)
- - Replace Nested Conditionals with Guard Clauses
cond-guard-clauses - - Replace Conditional with Polymorphism
cond-polymorphism - - Decompose Complex Conditionals
cond-decompose - - Consolidate Duplicate Conditional Fragments
cond-consolidate - - Introduce Special Case Object
cond-special-case - - Replace Conditional with Lookup Table
cond-lookup-table
- - 用卫语句替代嵌套条件
cond-guard-clauses - - 用多态替代条件判断
cond-polymorphism - - 分解复杂条件
cond-decompose - - 合并重复的条件片段
cond-consolidate - - 引入特殊情况对象
cond-special-case - - 用查找表替代条件判断
cond-lookup-table
5. Abstraction & Patterns (MEDIUM-HIGH)
5. 抽象与模式(中高)
- - Extract Strategy for Algorithm Variants
pattern-strategy - - Use Template Method for Shared Skeleton
pattern-template-method - - Use Factory for Complex Object Creation
pattern-factory - - Apply Open-Closed Principle
pattern-open-closed - - Prefer Composition Over Inheritance
pattern-composition-over-inheritance - - Extract Superclass for Common Behavior
pattern-extract-superclass
- - 为算法变体提取策略模式
pattern-strategy - - 使用模板方法实现共享骨架
pattern-template-method - - 使用工厂模式处理复杂对象创建
pattern-factory - - 应用开闭原则
pattern-open-closed - - 优先使用组合而非继承
pattern-composition-over-inheritance - - 提取超类以复用通用行为
pattern-extract-superclass
6. Data Organization (MEDIUM)
6. 数据组织(中)
- - Encapsulate Collection
data-encapsulate-collection - - Replace Primitive with Object
data-replace-primitive - - Encapsulate Record into Class
data-encapsulate-record - - Split Variable with Multiple Assignments
data-split-variable - - Replace Temp with Query
data-replace-temp-with-query
- - 封装集合
data-encapsulate-collection - - 用对象替代原始类型
data-replace-primitive - - 将记录封装为类
data-encapsulate-record - - 拆分存在多次赋值的变量
data-split-variable - - 用查询替代临时变量
data-replace-temp-with-query
7. Error Handling (MEDIUM)
7. 错误处理(中)
- - Use Exceptions Instead of Error Codes
error-exceptions-over-codes - - Create Domain-Specific Exception Types
error-custom-exceptions - - Fail Fast with Preconditions
error-fail-fast - - Separate Error Handling from Business Logic
error-separate-concerns
- - 使用异常而非错误码
error-exceptions-over-codes - - 创建领域特定的异常类型
error-custom-exceptions - - 通过前置条件快速失败
error-fail-fast - - 将错误处理与业务逻辑分离
error-separate-concerns
8. Micro-Refactoring (LOW)
8. 微重构(低)
- - Remove Dead Code
micro-remove-dead-code - - Inline Trivial Variables
micro-inline-variable - - Simplify Boolean Expressions
micro-simplify-expressions - - Rename for Clarity
micro-rename-for-clarity
- - 移除死代码
micro-remove-dead-code - - 内联琐碎变量
micro-inline-variable - - 简化布尔表达式
micro-simplify-expressions - - 为提升清晰度重命名
micro-rename-for-clarity
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
- Individual rules:
references/{prefix}-{slug}.md
阅读单个参考文件获取详细说明与代码示例:
- 章节定义 - 类别结构与影响级别说明
- 规则模板 - 添加新规则的模板
- 单个规则文件:
references/{prefix}-{slug}.md
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md包含所有规则详细内容的完整指南:
AGENTS.md