ruby-refactor

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Community Ruby Refactoring Best Practices

社区Ruby重构最佳实践

Comprehensive refactoring guide for Ruby applications, maintained by the community. Contains 45 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
这是一份由社区维护的Ruby应用全面重构指南,包含8个类别共45条规则,按影响优先级排序,可用于指导自动化重构和代码生成。

When to Apply

适用场景

Reference these guidelines when:
  • Refactoring Ruby code to reduce complexity and improve design
  • Extracting methods, classes, or value objects from large units
  • Simplifying complex conditionals and deep nesting
  • Reducing coupling between classes and modules
  • Adopting idiomatic Ruby patterns and modern Ruby 3.x features
当你遇到以下情况时,可参考本指南:
  • 重构Ruby代码以降低复杂度、优化设计
  • 从大单元中提取方法、类或值对象
  • 简化复杂条件语句和深层嵌套结构
  • 降低类与模块之间的耦合度
  • 采用Ruby惯用语法模式和Ruby 3.x新特性

Rule Categories by Priority

按优先级划分的规则类别

PriorityCategoryImpactPrefix
1Structure & DecompositionCRITICAL
struct-
2Conditional SimplificationCRITICAL
cond-
3Coupling & DependenciesHIGH
couple-
4Ruby IdiomsHIGH
idiom-
5Data & Value ObjectsMEDIUM-HIGH
data-
6Design PatternsMEDIUM
pattern-
7Modern Ruby 3.xMEDIUM
modern-
8Naming & ReadabilityLOW-MEDIUM
name-
优先级类别影响程度前缀
1结构与分解严重
struct-
2条件简化严重
cond-
3耦合与依赖
couple-
4Ruby惯用语法
idiom-
5数据与值对象中高
data-
6设计模式
pattern-
7现代Ruby 3.x
modern-
8命名与可读性低中
name-

Quick Reference

快速参考

1. Structure & Decomposition (CRITICAL)

1. 结构与分解(严重)

  • struct-extract-method
    - Extract Long Methods into Focused Units
  • struct-extract-class
    - Extract Class for Single Responsibility
  • struct-parameter-object
    - Introduce Parameter Object for Long Signatures
  • struct-compose-method
    - Compose Methods at Single Abstraction Level
  • struct-replace-method-with-object
    - Replace Complex Method with Method Object
  • struct-single-responsibility
    - One Reason to Change per Class
  • struct-flatten-deep-nesting
    - Flatten Deep Nesting with Early Extraction
  • struct-extract-method
    - 将长方法拆分为职责单一的单元
  • struct-extract-class
    - 提取类以遵循单一职责原则
  • struct-parameter-object
    - 引入参数对象处理长参数列表
  • struct-compose-method
    - 在同一抽象层级组合方法
  • struct-replace-method-with-object
    - 用方法对象替换复杂方法
  • struct-single-responsibility
    - 每个类仅保留一个变更理由
  • struct-flatten-deep-nesting
    - 通过提前提取扁平化深层嵌套结构

2. Conditional Simplification (CRITICAL)

2. 条件简化(严重)

  • cond-guard-clauses
    - Replace Nested Conditionals with Guard Clauses
  • cond-decompose-conditional
    - Extract Complex Booleans into Named Predicates
  • cond-replace-with-polymorphism
    - Replace case/when with Polymorphism
  • cond-null-object
    - Replace nil Checks with Null Object
  • cond-pattern-matching
    - Use Pattern Matching for Structural Conditions
  • cond-consolidate-duplicates
    - Consolidate Duplicate Conditional Fragments
  • cond-guard-clauses
    - 用卫语句替换嵌套条件
  • cond-decompose-conditional
    - 将复杂布尔逻辑提取为命名断言
  • cond-replace-with-polymorphism
    - 用多态替换case/when语句
  • cond-null-object
    - 用空对象模式替换nil检查
  • cond-pattern-matching
    - 使用模式匹配处理结构化条件
  • cond-consolidate-duplicates
    - 合并重复的条件片段

3. Coupling & Dependencies (HIGH)

3. 耦合与依赖(高)

  • couple-law-of-demeter
    - Enforce Law of Demeter with Delegation
  • couple-feature-envy
    - Move Method to Resolve Feature Envy
  • couple-dependency-injection
    - Inject Dependencies via Constructor Defaults
  • couple-composition-over-inheritance
    - Replace Mixin with Composed Object
  • couple-tell-dont-ask
    - Tell Objects What to Do, Don't Query Their State
  • couple-avoid-class-methods-domain
    - Avoid Class Methods in Domain Logic
  • couple-law-of-demeter
    - 通过委托遵循迪米特法则
  • couple-feature-envy
    - 移动方法解决特性羡慕问题
  • couple-dependency-injection
    - 通过构造函数默认值注入依赖
  • couple-composition-over-inheritance
    - 用组合对象替换Mixin
  • couple-tell-dont-ask
    - 告诉对象做什么,而非查询其状态
  • couple-avoid-class-methods-domain
    - 在领域逻辑中避免使用类方法

4. Ruby Idioms (HIGH)

4. Ruby惯用语法(高)

  • idiom-prefer-enumerable
    - Use map/select/reject Over each with Accumulator
  • idiom-keyword-arguments
    - Use Keyword Arguments for Clarity
  • idiom-duck-typing
    - Use respond_to? Over is_a? for Type Checking
  • idiom-predicate-methods
    - Name Boolean Methods with ? Suffix
  • idiom-respond-to-missing
    - Always Pair method_missing with respond_to_missing?
  • idiom-block-yield
    - Use yield Over block.call for Simple Blocks
  • idiom-implicit-return
    - Omit Explicit return for Last Expression
  • idiom-prefer-enumerable
    - 使用map/select/reject替代带累加器的each
  • idiom-keyword-arguments
    - 使用关键字参数提升可读性
  • idiom-duck-typing
    - 使用respond_to?替代is_a?做类型检查
  • idiom-predicate-methods
    - 布尔方法以?后缀命名
  • idiom-respond-to-missing
    - 始终将method_missing与respond_to_missing?配对使用
  • idiom-block-yield
    - 简单块使用yield替代block.call
  • idiom-implicit-return
    - 省略最后表达式的显式return

5. Data & Value Objects (MEDIUM-HIGH)

5. 数据与值对象(中高)

  • data-value-object
    - Replace Primitive Obsession with Value Objects
  • data-define-immutable
    - Use Data.define for Immutable Value Objects
  • data-encapsulate-collection
    - Encapsulate Collections Behind Domain Methods
  • data-replace-data-clump
    - Replace Data Clumps with Grouped Objects
  • data-separate-query-command
    - Separate Query Methods from Command Methods
  • data-value-object
    - 用值对象替换基本类型偏执
  • data-define-immutable
    - 使用Data.define创建不可变值对象
  • data-encapsulate-collection
    - 在领域方法后封装集合
  • data-replace-data-clump
    - 用分组对象替换数据团
  • data-separate-query-command
    - 将查询方法与命令方法分离

6. Design Patterns (MEDIUM)

6. 设计模式(中)

  • pattern-strategy
    - Extract Algorithm Variations into Strategy Objects
  • pattern-factory
    - Use Factory Method to Abstract Object Creation
  • pattern-template-method
    - Define Algorithm Skeleton with Template Method
  • pattern-decorator
    - Wrap Objects with Decorator for Added Behavior
  • pattern-null-object-protocol
    - Implement Null Object with Full Protocol
  • pattern-strategy
    - 将算法变体提取为策略对象
  • pattern-factory
    - 使用工厂方法抽象对象创建逻辑
  • pattern-template-method
    - 用模板方法定义算法骨架
  • pattern-decorator
    - 用装饰器包装对象以添加行为
  • pattern-null-object-protocol
    - 实现带完整协议的空对象

7. Modern Ruby 3.x (MEDIUM)

7. 现代Ruby 3.x(中)

  • modern-pattern-matching
    - Use case/in for Structural Pattern Matching
  • modern-deconstruct-keys
    - Implement deconstruct_keys for Custom Pattern Matching
  • modern-endless-methods
    - Use Endless Method Definition for Simple Methods
  • modern-hash-pattern-guard
    - Use Pattern Matching with Guard Clauses
  • modern-rightward-assignment
    - Use Rightward Assignment for Pipeline Expressions
  • modern-pattern-matching
    - 使用case/in进行结构化模式匹配
  • modern-deconstruct-keys
    - 实现deconstruct_keys支持自定义模式匹配
  • modern-endless-methods
    - 用无限方法定义简化简单方法
  • modern-hash-pattern-guard
    - 结合卫语句使用模式匹配
  • modern-rightward-assignment
    - 用右向赋值实现管道表达式

8. Naming & Readability (LOW-MEDIUM)

8. 命名与可读性(低中)

  • name-intention-revealing
    - Use Intention-Revealing Names
  • name-consistent-vocabulary
    - Use One Word per Concept Across Codebase
  • name-avoid-abbreviations
    - Spell Out Names Except Universal Abbreviations
  • name-rename-to-remove-comments
    - Rename to Eliminate Need for Comments
  • name-intention-revealing
    - 使用能体现意图的命名
  • name-consistent-vocabulary
    - 整个代码库中同一概念使用统一词汇
  • name-avoid-abbreviations
    - 拼写完整名称,通用缩写除外
  • name-rename-to-remove-comments
    - 通过重命名消除注释需求

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
阅读单个参考文件获取详细说明和代码示例:
  • 章节定义 - 类别结构和影响级别说明
  • 规则模板 - 添加新规则的模板

Reference Files

参考文件

FileDescription
references/_sections.mdCategory definitions and ordering
assets/templates/_template.mdTemplate for new rules
metadata.jsonVersion and reference information
文件描述
references/_sections.md类别定义与排序说明
assets/templates/_template.md新规则模板
metadata.json版本与参考信息