clean-code
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRobert C. Martin (Uncle Bob) Clean Code Best Practices
Robert C. Martin(鲍勃大叔)Clean Code最佳实践
Comprehensive software craftsmanship guide based on Robert C. Martin's "Clean Code: A Handbook of Agile Software Craftsmanship". Contains 45 rules across 8 categories, prioritized by impact to guide code reviews, refactoring decisions, and new development.
这是基于Robert C. Martin所著《Clean Code: A Handbook of Agile Software Craftsmanship》的综合性软件工艺指南。包含8个类别共45条规则,按影响优先级排序,可指导代码评审、重构决策和新代码开发。
When to Apply
适用场景
Reference these guidelines when:
- Writing new functions, classes, or modules
- Naming variables, functions, classes, or files
- Reviewing code for maintainability issues
- Refactoring existing code to improve clarity
- Writing or improving unit tests
在以下场景中参考这些指南:
- 编写新函数、类或模块
- 为变量、函数、类或文件命名
- 评审代码以排查可维护性问题
- 重构现有代码以提升清晰度
- 编写或优化单元测试
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Meaningful Names | CRITICAL | |
| 2 | Functions | CRITICAL | |
| 3 | Comments | HIGH | |
| 4 | Formatting | HIGH | |
| 5 | Objects and Data Structures | MEDIUM-HIGH | |
| 6 | Error Handling | MEDIUM-HIGH | |
| 7 | Unit Tests | MEDIUM | |
| 8 | Classes and Systems | MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 有意义的命名 | 关键 | |
| 2 | 函数 | 关键 | |
| 3 | 注释 | 高 | |
| 4 | 格式 | 高 | |
| 5 | 对象与数据结构 | 中高 | |
| 6 | 错误处理 | 中高 | |
| 7 | 单元测试 | 中 | |
| 8 | 类与系统 | 中 | |
Quick Reference
快速参考
1. Meaningful Names (CRITICAL)
1. 有意义的命名(关键)
- - Use names that reveal intent
name-intention-revealing - - Avoid misleading names
name-avoid-disinformation - - Make meaningful distinctions
name-meaningful-distinctions - - Use pronounceable names
name-pronounceable - - Use searchable names
name-searchable - - Avoid encodings in names
name-avoid-encodings - - Use noun phrases for class names
name-class-noun - - Use verb phrases for method names
name-method-verb
- - 使用能体现意图的命名
name-intention-revealing - - 避免误导性命名
name-avoid-disinformation - - 做出有意义的区分
name-meaningful-distinctions - - 使用易读的命名
name-pronounceable - - 使用便于搜索的命名
name-searchable - - 避免在命名中使用编码
name-avoid-encodings - - 类名使用名词短语
name-class-noun - - 方法名使用动词短语
name-method-verb
2. Functions (CRITICAL)
2. 函数(关键)
- - Keep functions small
func-small - - Functions should do one thing
func-one-thing - - Maintain one level of abstraction
func-abstraction-level - - Minimize function arguments
func-minimize-arguments - - Avoid side effects
func-no-side-effects - - Separate commands from queries
func-command-query-separation - - Prefer exceptions to error codes
func-prefer-exceptions - - Do not repeat yourself
func-dry
- - 保持函数简洁
func-small - - 函数应该只做一件事
func-one-thing - - 保持单一抽象层级
func-abstraction-level - - 尽量减少函数参数
func-minimize-arguments - - 避免副作用
func-no-side-effects - - 分离命令与查询
func-command-query-separation - - 优先使用异常而非错误码
func-prefer-exceptions - - 不要重复自己(DRY原则)
func-dry
3. Comments (HIGH)
3. 注释(高)
- - Express yourself in code, not comments
cmt-express-in-code - - Use comments to explain intent
cmt-explain-intent - - Avoid redundant comments
cmt-avoid-redundant - - Delete commented-out code
cmt-avoid-commented-out-code - - Use warning comments for consequences
cmt-warning-consequences
- - 用代码表达,而非注释
cmt-express-in-code - - 使用注释解释意图
cmt-explain-intent - - 避免冗余注释
cmt-avoid-redundant - - 删除被注释掉的代码
cmt-avoid-commented-out-code - - 使用警告注释说明后果
cmt-warning-consequences
4. Formatting (HIGH)
4. 格式(高)
- - Use vertical formatting for readability
fmt-vertical-formatting - - Avoid horizontal alignment
fmt-horizontal-alignment - - Follow team formatting rules
fmt-team-rules - - Respect indentation rules
fmt-indentation
- - 使用垂直格式提升可读性
fmt-vertical-formatting - - 避免水平对齐
fmt-horizontal-alignment - - 遵循团队格式规则
fmt-team-rules - - 遵守缩进规则
fmt-indentation
5. Objects and Data Structures (MEDIUM-HIGH)
5. 对象与数据结构(中高)
- - Hide data behind abstractions
obj-data-abstraction - - Understand data/object anti-symmetry
obj-data-object-asymmetry - - Follow the Law of Demeter
obj-law-of-demeter - - Avoid hybrid data-object structures
obj-avoid-hybrids - - Use DTOs for data transfer
obj-dto
- - 用抽象隐藏数据
obj-data-abstraction - - 理解数据/对象的不对称性
obj-data-object-asymmetry - - 遵循Law of Demeter(迪米特法则)
obj-law-of-demeter - - 避免混合数据-对象结构
obj-avoid-hybrids - - 使用DTO进行数据传输
obj-dto
6. Error Handling (MEDIUM-HIGH)
6. 错误处理(中高)
- - Use exceptions instead of return codes
err-use-exceptions - - Write try-catch-finally first
err-write-try-catch-first - - Provide context with exceptions
err-provide-context - - Define exceptions by caller needs
err-define-by-caller-needs - - Avoid returning and passing null
err-avoid-null
- - 使用异常而非返回码
err-use-exceptions - - 先编写try-catch-finally块
err-write-try-catch-first - - 为异常提供上下文信息
err-provide-context - - 根据调用者需求定义异常
err-define-by-caller-needs - - 避免返回和传递null
err-avoid-null
7. Unit Tests (MEDIUM)
7. 单元测试(中)
- - Follow the three laws of TDD
test-first-law - - Keep tests clean
test-keep-clean - - One assert per test
test-one-assert - - Follow FIRST principles
test-first-principles - - Use Build-Operate-Check pattern
test-build-operate-check
- - 遵循TDD的三条法则
test-first-law - - 保持测试代码整洁
test-keep-clean - - 每个测试只包含一个断言
test-one-assert - - 遵循FIRST原则
test-first-principles - - 使用Build-Operate-Check模式
test-build-operate-check
8. Classes and Systems (MEDIUM)
8. 类与系统(中)
- - Keep classes small
class-small - - Maintain class cohesion
class-cohesion - - Organize classes for change
class-organize-for-change - - Isolate classes from change
class-isolate-from-change - - Separate construction from use
class-separate-concerns
- - 保持类的简洁
class-small - - 保持类的内聚性
class-cohesion - - 为变更组织类结构
class-organize-for-change - - 将类与变更隔离开
class-isolate-from-change - - 分离构造与使用
class-separate-concerns
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 | 版本与参考信息 |