ruby-refactor
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCommunity 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
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Structure & Decomposition | CRITICAL | |
| 2 | Conditional Simplification | CRITICAL | |
| 3 | Coupling & Dependencies | HIGH | |
| 4 | Ruby Idioms | HIGH | |
| 5 | Data & Value Objects | MEDIUM-HIGH | |
| 6 | Design Patterns | MEDIUM | |
| 7 | Modern Ruby 3.x | MEDIUM | |
| 8 | Naming & Readability | LOW-MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 结构与分解 | 严重 | |
| 2 | 条件简化 | 严重 | |
| 3 | 耦合与依赖 | 高 | |
| 4 | Ruby惯用语法 | 高 | |
| 5 | 数据与值对象 | 中高 | |
| 6 | 设计模式 | 中 | |
| 7 | 现代Ruby 3.x | 中 | |
| 8 | 命名与可读性 | 低中 | |
Quick Reference
快速参考
1. Structure & Decomposition (CRITICAL)
1. 结构与分解(严重)
- - Extract Long Methods into Focused Units
struct-extract-method - - Extract Class for Single Responsibility
struct-extract-class - - Introduce Parameter Object for Long Signatures
struct-parameter-object - - Compose Methods at Single Abstraction Level
struct-compose-method - - Replace Complex Method with Method Object
struct-replace-method-with-object - - One Reason to Change per Class
struct-single-responsibility - - Flatten Deep Nesting with Early Extraction
struct-flatten-deep-nesting
- - 将长方法拆分为职责单一的单元
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. 条件简化(严重)
- - Replace Nested Conditionals with Guard Clauses
cond-guard-clauses - - Extract Complex Booleans into Named Predicates
cond-decompose-conditional - - Replace case/when with Polymorphism
cond-replace-with-polymorphism - - Replace nil Checks with Null Object
cond-null-object - - Use Pattern Matching for Structural Conditions
cond-pattern-matching - - Consolidate Duplicate Conditional Fragments
cond-consolidate-duplicates
- - 用卫语句替换嵌套条件
cond-guard-clauses - - 将复杂布尔逻辑提取为命名断言
cond-decompose-conditional - - 用多态替换case/when语句
cond-replace-with-polymorphism - - 用空对象模式替换nil检查
cond-null-object - - 使用模式匹配处理结构化条件
cond-pattern-matching - - 合并重复的条件片段
cond-consolidate-duplicates
3. Coupling & Dependencies (HIGH)
3. 耦合与依赖(高)
- - Enforce Law of Demeter with Delegation
couple-law-of-demeter - - Move Method to Resolve Feature Envy
couple-feature-envy - - Inject Dependencies via Constructor Defaults
couple-dependency-injection - - Replace Mixin with Composed Object
couple-composition-over-inheritance - - Tell Objects What to Do, Don't Query Their State
couple-tell-dont-ask - - Avoid Class Methods in Domain Logic
couple-avoid-class-methods-domain
- - 通过委托遵循迪米特法则
couple-law-of-demeter - - 移动方法解决特性羡慕问题
couple-feature-envy - - 通过构造函数默认值注入依赖
couple-dependency-injection - - 用组合对象替换Mixin
couple-composition-over-inheritance - - 告诉对象做什么,而非查询其状态
couple-tell-dont-ask - - 在领域逻辑中避免使用类方法
couple-avoid-class-methods-domain
4. Ruby Idioms (HIGH)
4. Ruby惯用语法(高)
- - Use map/select/reject Over each with Accumulator
idiom-prefer-enumerable - - Use Keyword Arguments for Clarity
idiom-keyword-arguments - - Use respond_to? Over is_a? for Type Checking
idiom-duck-typing - - Name Boolean Methods with ? Suffix
idiom-predicate-methods - - Always Pair method_missing with respond_to_missing?
idiom-respond-to-missing - - Use yield Over block.call for Simple Blocks
idiom-block-yield - - Omit Explicit return for Last Expression
idiom-implicit-return
- - 使用map/select/reject替代带累加器的each
idiom-prefer-enumerable - - 使用关键字参数提升可读性
idiom-keyword-arguments - - 使用respond_to?替代is_a?做类型检查
idiom-duck-typing - - 布尔方法以?后缀命名
idiom-predicate-methods - - 始终将method_missing与respond_to_missing?配对使用
idiom-respond-to-missing - - 简单块使用yield替代block.call
idiom-block-yield - - 省略最后表达式的显式return
idiom-implicit-return
5. Data & Value Objects (MEDIUM-HIGH)
5. 数据与值对象(中高)
- - Replace Primitive Obsession with Value Objects
data-value-object - - Use Data.define for Immutable Value Objects
data-define-immutable - - Encapsulate Collections Behind Domain Methods
data-encapsulate-collection - - Replace Data Clumps with Grouped Objects
data-replace-data-clump - - Separate Query Methods from Command Methods
data-separate-query-command
- - 用值对象替换基本类型偏执
data-value-object - - 使用Data.define创建不可变值对象
data-define-immutable - - 在领域方法后封装集合
data-encapsulate-collection - - 用分组对象替换数据团
data-replace-data-clump - - 将查询方法与命令方法分离
data-separate-query-command
6. Design Patterns (MEDIUM)
6. 设计模式(中)
- - Extract Algorithm Variations into Strategy Objects
pattern-strategy - - Use Factory Method to Abstract Object Creation
pattern-factory - - Define Algorithm Skeleton with Template Method
pattern-template-method - - Wrap Objects with Decorator for Added Behavior
pattern-decorator - - Implement Null Object with Full Protocol
pattern-null-object-protocol
- - 将算法变体提取为策略对象
pattern-strategy - - 使用工厂方法抽象对象创建逻辑
pattern-factory - - 用模板方法定义算法骨架
pattern-template-method - - 用装饰器包装对象以添加行为
pattern-decorator - - 实现带完整协议的空对象
pattern-null-object-protocol
7. Modern Ruby 3.x (MEDIUM)
7. 现代Ruby 3.x(中)
- - Use case/in for Structural Pattern Matching
modern-pattern-matching - - Implement deconstruct_keys for Custom Pattern Matching
modern-deconstruct-keys - - Use Endless Method Definition for Simple Methods
modern-endless-methods - - Use Pattern Matching with Guard Clauses
modern-hash-pattern-guard - - Use Rightward Assignment for Pipeline Expressions
modern-rightward-assignment
- - 使用case/in进行结构化模式匹配
modern-pattern-matching - - 实现deconstruct_keys支持自定义模式匹配
modern-deconstruct-keys - - 用无限方法定义简化简单方法
modern-endless-methods - - 结合卫语句使用模式匹配
modern-hash-pattern-guard - - 用右向赋值实现管道表达式
modern-rightward-assignment
8. Naming & Readability (LOW-MEDIUM)
8. 命名与可读性(低中)
- - Use Intention-Revealing Names
name-intention-revealing - - Use One Word per Concept Across Codebase
name-consistent-vocabulary - - Spell Out Names Except Universal Abbreviations
name-avoid-abbreviations - - Rename to Eliminate Need for Comments
name-rename-to-remove-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
参考文件
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 描述 |
|---|---|
| references/_sections.md | 类别定义与排序说明 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |