Uncle Bob — Clean Code & Architecture Principles
Apply these principles when writing, reviewing, or refactoring code. They are not rules to follow blindly — use judgment, but default to clean.
The Boy Scout Rule
Leave the code cleaner than you found it. Every commit should improve the codebase, even if slightly.
Clean Code Fundamentals
Naming
- - Names reveal intent. If a name requires a comment, the name is wrong.
- Use pronounceable, searchable names. Avoid abbreviations, single letters (except loop counters), and prefixes.
- Classes/types: noun or noun phrase (
AccountManager, OrderRepository). - Functions/methods: verb or verb phrase (
calculateTotal, fetchUser, isValid). - Booleans: read as a question (
isActive, hasPermission, canExecute). - Avoid mental mapping.
r is not a URL. Say url.
Functions
- - Small. Then smaller. A function does one thing.
- Ideally 0-2 arguments. 3+ is a smell — extract an options object or rethink the design.
- No side effects. A function named
checkPassword must not also initialize a session. - Command-Query Separation: a function either does something (command) or answers something (query), never both.
- Don't Repeat Yourself (DRY) — but don't abstract prematurely. Three instances of duplication is the threshold.
- Extract till you drop: if you can extract a meaningful sub-function, do it.
Comments
- - Good code is self-documenting. Comments compensate for failure to express in code.
- Legal, informative, clarifying intent, warning of consequences, and TODO comments are acceptable.
- Delete commented-out code. Version control remembers.
- Never write comments that restate what the code does (
// increment i before i++).
Formatting
- - Vertical: newspaper metaphor — high-level summary at top, details below.
- Related functions stay close. Caller above callee.
- Horizontal: avoid scrolling. Keep lines short.
- Consistent formatting across the team trumps personal preference.
Error Handling
- - Prefer exceptions/Result types over error codes.
- Don't return null. Don't pass null.
- Write try-catch at the top level of a function, not scattered throughout.
- Error handling is one thing — a function that handles errors should do little else.
- Define exception classes in terms of the caller's needs, not the thrower's implementation.
Objects vs. Data Structures
- - Objects hide data, expose behavior. Data structures expose data, have no behavior.
- Don't mix them. A class with public fields AND business methods is the worst of both worlds.
- Law of Demeter: a method should only call methods on its own object, its parameters, objects it creates, or its direct dependencies. No
a.getB().getC().doThing().
SOLID Principles
For detailed explanations and examples, see references/solid.md.
- - S — Single Responsibility: A class has one reason to change. One actor, one responsibility.
- O — Open/Closed: Open for extension, closed for modification. Use polymorphism, not conditionals.
- L — Liskov Substitution: Subtypes must be substitutable for their base types without breaking behavior.
- I — Interface Segregation: Many specific interfaces beat one general-purpose interface. Clients should not depend on methods they don't use.
- D — Dependency Inversion: Depend on abstractions, not concretions. High-level modules must not depend on low-level modules.
Clean Architecture
For the full architecture guide, see references/clean-architecture.md.
The Dependency Rule
Source code dependencies must point inward — toward higher-level policies.
CODEBLOCK0
- - Entities: enterprise business rules, pure domain objects.
- Use Cases: application-specific business rules (orchestrate entities).
- Interface Adapters: convert between use case format and external format (controllers, presenters, gateways).
- Frameworks & Drivers: the outermost layer (DB, web framework, UI). Details. Replaceable.
Key Rules
- - Nothing in an inner circle knows about anything in an outer circle.
- Data crossing boundaries is simple DTOs or value objects — never framework-specific types.
- The database is a detail. The web is a detail. Frameworks are details.
Component Principles
- - Common Closure Principle (CCP): classes that change together belong together.
- Common Reuse Principle (CRP): don't force users to depend on things they don't use.
- Stable Dependencies Principle: depend in the direction of stability.
- Stable Abstractions Principle: stable components should be abstract.
Code Smells (Red Flags)
- - Rigidity: small change causes cascade of changes elsewhere.
- Fragility: change in one place breaks unrelated code.
- Immobility: can't reuse a module without dragging its dependencies.
- Needless complexity: speculative generality, premature abstraction.
- Needless repetition: copy-paste code (DRY violation).
- Opacity: code is hard to understand.
- Long functions, large classes, long parameter lists, boolean flags, switch/case on type.
Testing (TDD)
- - Three Laws of TDD: (1) Write a failing test first. (2) Write only enough test to fail. (3) Write only enough production code to pass.
- Tests are first-class code. Keep them clean, readable, fast.
- One assert per test (guideline, not dogma). One concept per test.
- F.I.R.S.T.: Fast, Independent, Repeatable, Self-validating, Timely.
- Test boundaries, not implementations. Test behavior, not methods.
Applying These Principles
When reviewing or writing code, check in this order:
- 1. Readability: Can someone understand this in 30 seconds?
- Naming: Do names reveal intent?
- Function size: Can anything be extracted?
- Single Responsibility: Does each unit have one reason to change?
- Dependencies: Do they point toward stability/abstraction?
- Coupling: Is anything unnecessarily coupled?
- Error handling: Is it clean and consistent?
- Tests: Are they present, clean, and testing behavior?
Uncle Bob — 整洁代码与架构原则
在编写、审查或重构代码时应用这些原则。它们不是盲目遵循的规则——请运用判断力,但默认保持整洁。
童子军规则
让代码比你发现时更整洁。每次提交都应改善代码库,哪怕只是微小的改进。
整洁代码基础
命名
- - 名称应揭示意图。如果名称需要注释,说明名称有问题。
- 使用可发音、可搜索的名称。避免缩写、单个字母(循环计数器除外)和前缀。
- 类/类型:名词或名词短语(AccountManager、OrderRepository)。
- 函数/方法:动词或动词短语(calculateTotal、fetchUser、isValid)。
- 布尔值:读起来像一个问题(isActive、hasPermission、canExecute)。
- 避免心理映射。r 不是 URL。请用 url。
函数
- - 要小。然后更小。一个函数只做一件事。
- 理想情况下有 0-2 个参数。3 个以上是坏味道——提取一个选项对象或重新思考设计。
- 没有副作用。名为 checkPassword 的函数绝不能同时初始化会话。
- 命令-查询分离:函数要么执行操作(命令),要么回答问题(查询),不能两者兼有。
- 不要重复自己(DRY)——但不要过早抽象。三次重复是阈值。
- 能提取就提取:如果可以提取出有意义的子函数,就去做。
注释
- - 好的代码是自文档化的。注释是为了弥补代码表达上的不足。
- 法律声明、信息性说明、澄清意图、警告后果以及 TODO 注释是可以接受的。
- 删除被注释掉的代码。版本控制系统会记住它们。
- 永远不要写重复代码功能的注释(在 i++ 前写 // 递增 i)。
格式
- - 垂直方向:报纸隐喻——高层摘要放在顶部,细节放在下面。
- 相关函数保持靠近。调用者在上,被调用者在下。
- 水平方向:避免滚动。保持行长度简短。
- 团队内一致的格式优于个人偏好。
错误处理
- - 优先使用异常/Result 类型而非错误码。
- 不要返回 null。不要传递 null。
- 在函数的顶层编写 try-catch,不要分散在各处。
- 错误处理是一件事——处理错误的函数应尽量少做其他事情。
- 根据调用者的需求定义异常类,而非抛出者的实现。
对象与数据结构
- - 对象隐藏数据,暴露行为。数据结构暴露数据,没有行为。
- 不要混用它们。同时拥有公共字段和业务方法的类是最糟糕的。
- 迪米特法则:一个方法只应调用其自身对象、参数、创建的对象或直接依赖的方法。不要出现 a.getB().getC().doThing()。
SOLID 原则
详细解释和示例请参见 references/solid.md。
- - S — 单一职责:一个类只有一个变更理由。一个角色,一个职责。
- O — 开闭原则:对扩展开放,对修改关闭。使用多态,而非条件判断。
- L — 里氏替换:子类型必须能够替换其基类型而不破坏行为。
- I — 接口隔离:多个特定接口优于一个通用接口。客户端不应依赖它们不用的方法。
- D — 依赖反转:依赖抽象,而非具体实现。高层模块不应依赖低层模块。
整洁架构
完整架构指南请参见 references/clean-architecture.md。
依赖规则
源代码依赖必须指向内部——朝向更高层的策略。
框架与驱动 → 接口适配器 → 用例 → 实体
(外层) (内层)
- - 实体:企业业务规则,纯领域对象。
- 用例:应用特定的业务规则(编排实体)。
- 接口适配器:在用例格式和外部格式之间转换(控制器、展示器、网关)。
- 框架与驱动:最外层(数据库、Web 框架、UI)。细节。可替换。
关键规则
- - 内圈中的任何内容都不知道外圈中的任何内容。
- 跨越边界的数据是简单的 DTO 或值对象——绝不能是框架特定的类型。
- 数据库是细节。Web 是细节。框架是细节。
组件原则
- - 共同闭包原则(CCP):一起变更的类应放在一起。
- 共同复用原则(CRP):不要强迫用户依赖他们不用的东西。
- 稳定依赖原则:朝着稳定的方向依赖。
- 稳定抽象原则:稳定的组件应该是抽象的。
代码坏味道(危险信号)
- - 僵化性:小变更导致其他地方产生连锁变更。
- 脆弱性:一个地方的变更破坏了不相关的代码。
- 不可移动性:无法在不拖带其依赖的情况下复用模块。
- 不必要的复杂性:投机性泛化,过早抽象。
- 不必要的重复:复制粘贴代码(违反 DRY)。
- 不透明性:代码难以理解。
- 长函数、大类、长参数列表、布尔标志、基于类型的 switch/case。
测试(TDD)
- - TDD 三定律:(1)先编写一个失败的测试。(2)只编写足够让测试失败的代码。(3)只编写足够让测试通过的 production 代码。
- 测试是一等代码。保持它们整洁、可读、快速。
- 每个测试一个断言(指导原则,非教条)。每个测试一个概念。
- F.I.R.S.T.:快速(Fast)、独立(Independent)、可重复(Repeatable)、自验证(Self-validating)、及时(Timely)。
- 测试边界,而非实现。测试行为,而非方法。
应用这些原则
在审查或编写代码时,按此顺序检查:
- 1. 可读性:别人能在 30 秒内理解吗?
- 命名:名称是否揭示了意图?
- 函数大小:有什么可以提取的吗?
- 单一职责:每个单元是否只有一个变更理由?
- 依赖:它们是否指向稳定/抽象的方向?
- 耦合:是否有不必要的耦合?
- 错误处理:是否整洁且一致?
- 测试:是否存在、整洁且测试了行为?