refactor

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Fowler/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

按优先级划分的规则类别

PriorityCategoryImpactPrefix
1Structure & DecompositionCRITICAL
struct-
2Coupling & DependenciesCRITICAL
couple-
3Naming & ClarityHIGH
name-
4Conditional LogicHIGH
cond-
5Abstraction & PatternsMEDIUM-HIGH
pattern-
6Data OrganizationMEDIUM
data-
7Error HandlingMEDIUM
error-
8Micro-RefactoringLOW
micro-
优先级类别影响程度前缀
1结构与分解关键
struct-
2耦合与依赖关键
couple-
3命名与清晰度
name-
4条件逻辑
cond-
5抽象与模式中高
pattern-
6数据组织
data-
7错误处理
error-
8微重构
micro-

Quick Reference

快速参考

1. Structure & Decomposition (CRITICAL)

1. 结构与分解(关键)

  • struct-extract-method
    - Extract Method for Long Functions
  • struct-single-responsibility
    - Apply Single Responsibility Principle
  • struct-extract-class
    - Extract Class from Large Class
  • struct-compose-method
    - Compose Method for Readable Flow
  • struct-function-length
    - Keep Functions Under 20 Lines
  • struct-replace-method-with-object
    - Replace Method with Method Object
  • struct-parameter-object
    - Introduce Parameter Object
  • struct-extract-method
    - 为长函数提取方法
  • struct-single-responsibility
    - 应用单一职责原则
  • struct-extract-class
    - 从大型类中提取类
  • struct-compose-method
    - 组合方法以实现可读流程
  • struct-function-length
    - 函数代码行数控制在20行以内
  • struct-replace-method-with-object
    - 用方法对象替代方法
  • struct-parameter-object
    - 引入参数对象

2. Coupling & Dependencies (CRITICAL)

2. 耦合与依赖(关键)

  • couple-dependency-injection
    - Use Dependency Injection
  • couple-hide-delegate
    - Hide Delegate to Reduce Coupling
  • couple-remove-middle-man
    - Remove Middle Man When Excessive
  • couple-feature-envy
    - Fix Feature Envy by Moving Methods
  • couple-interface-segregation
    - Apply Interface Segregation Principle
  • couple-preserve-whole-object
    - Preserve Whole Object Instead of Fields
  • 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. 命名与清晰度(高)

  • name-intention-revealing
    - Use Intention-Revealing Names
  • name-avoid-abbreviations
    - Avoid Abbreviations and Acronyms
  • name-consistent-vocabulary
    - Use Consistent Vocabulary
  • name-searchable-names
    - Use Searchable Names
  • name-avoid-encodings
    - Avoid Type Encodings in Names
  • name-intention-revealing
    - 使用能表达意图的命名
  • name-avoid-abbreviations
    - 避免缩写与首字母缩写词
  • name-consistent-vocabulary
    - 使用一致的术语词汇
  • name-searchable-names
    - 使用便于搜索的命名
  • name-avoid-encodings
    - 避免在命名中使用类型编码

4. Conditional Logic (HIGH)

4. 条件逻辑(高)

  • cond-guard-clauses
    - Replace Nested Conditionals with Guard Clauses
  • cond-polymorphism
    - Replace Conditional with Polymorphism
  • cond-decompose
    - Decompose Complex Conditionals
  • cond-consolidate
    - Consolidate Duplicate Conditional Fragments
  • cond-special-case
    - Introduce Special Case Object
  • cond-lookup-table
    - Replace Conditional with Lookup Table
  • cond-guard-clauses
    - 用卫语句替代嵌套条件
  • cond-polymorphism
    - 用多态替代条件判断
  • cond-decompose
    - 分解复杂条件
  • cond-consolidate
    - 合并重复的条件片段
  • cond-special-case
    - 引入特殊情况对象
  • cond-lookup-table
    - 用查找表替代条件判断

5. Abstraction & Patterns (MEDIUM-HIGH)

5. 抽象与模式(中高)

  • pattern-strategy
    - Extract Strategy for Algorithm Variants
  • pattern-template-method
    - Use Template Method for Shared Skeleton
  • pattern-factory
    - Use Factory for Complex Object Creation
  • pattern-open-closed
    - Apply Open-Closed Principle
  • pattern-composition-over-inheritance
    - Prefer Composition Over Inheritance
  • pattern-extract-superclass
    - Extract Superclass for Common Behavior
  • pattern-strategy
    - 为算法变体提取策略模式
  • pattern-template-method
    - 使用模板方法实现共享骨架
  • pattern-factory
    - 使用工厂模式处理复杂对象创建
  • pattern-open-closed
    - 应用开闭原则
  • pattern-composition-over-inheritance
    - 优先使用组合而非继承
  • pattern-extract-superclass
    - 提取超类以复用通用行为

6. Data Organization (MEDIUM)

6. 数据组织(中)

  • data-encapsulate-collection
    - Encapsulate Collection
  • data-replace-primitive
    - Replace Primitive with Object
  • data-encapsulate-record
    - Encapsulate Record into Class
  • data-split-variable
    - Split Variable with Multiple Assignments
  • data-replace-temp-with-query
    - 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. 错误处理(中)

  • error-exceptions-over-codes
    - Use Exceptions Instead of Error Codes
  • error-custom-exceptions
    - Create Domain-Specific Exception Types
  • error-fail-fast
    - Fail Fast with Preconditions
  • error-separate-concerns
    - Separate Error Handling from Business Logic
  • error-exceptions-over-codes
    - 使用异常而非错误码
  • error-custom-exceptions
    - 创建领域特定的异常类型
  • error-fail-fast
    - 通过前置条件快速失败
  • error-separate-concerns
    - 将错误处理与业务逻辑分离

8. Micro-Refactoring (LOW)

8. 微重构(低)

  • micro-remove-dead-code
    - Remove Dead Code
  • micro-inline-variable
    - Inline Trivial Variables
  • micro-simplify-expressions
    - Simplify Boolean Expressions
  • micro-rename-for-clarity
    - 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