[美] 罗伯特·C. 马丁(Robert C. Martin):
软件开发大师,设计模式和敏捷开发先驱,敏捷联盟首任主席,C++ Report前主编,被后辈程序员尊称为“Bob大叔”。20世纪70年代初成为职业程序员,后创办Object Mentor公司并任总裁。Martin还是一名多产的作家,至今已发表数百篇文章、论文和博客文章。除本书外,还著有《代码整洁之道:程序员的职业素养》《敏捷软件开发:原则、模式和实践》《UML:Java程序员指南》等。
目录
Chapter 1:Clean Code / 整洁代码 1
There Will Be Code / 要有代码 2
Bad Code / 糟糕的代码 3
The Total Cost of Owning a Mess / 混乱的代价 4
Schools of Thought / 思想流派 12
We Are Authors / 我们是作者 13
The Boy Scout Rule / 童子军军规 14
Prequel and Principles / 前传与原则 15
Conclusion / 小结 15
Bibliography / 文献 15
Chapter 2:Meaningful Names / 有意义的命名 17
Introduction / 介绍 17
Use Intention-Revealing Names / 名副其实 18
Avoid Disinformation / 避免误导 19
Make Meaningful Distinctions / 做有意义的区分 20
Use Pronounceable Names / 使用读得出来的名称 21
Use Searchable Names / 使用可搜索的名称 22
Avoid Encodings / 避免使用编码 23
Avoid Mental Mapping / 避免思维映射 25
Class Names / 类名 25
Method Names / 方法名 25
Don’t Be Cute / 别抖机灵 26
Pick One Word per Concept / 每个概念对应一个词 26
Don’t Pun / 别用双关语 26
Use Solution Domain Names / 使用解决方案领域名称 27
Use Problem Domain Names / 使用源自所涉问题领域的名称 27
Add Meaningful Context / 添加有意义的语境 27
Don’t Add Gratuitous Context / 不要添加没用的语境 29
Final Words / 最后的话 30
Chapter 3:Functions / 函数 31
Small! / 短小 34
Do One Thing 只做一件事 35
One Level of Abstraction per Function / 每个函数一个抽象层级 36
Switch Statements / switch 语句 37
Use Descriptive Names / 使用具有描述性的名称 39
Function Arguments / 函数参数 40
Have No Side Effects / 无副作用 44
Command Query Separation / 分隔指令与询问 45
Prefer Exceptions to Returning Error Codes / 使用异常替代返回错误码 46
Don’t Repeat Yourself / 别重复自己 48
Structured Programming / 结构化编程 48
How Do You Write Functions Like This / 如何写出这样的函数 49
Conclusion / 小结 49
SetupTeardownIncluder / SetupTeardownIncluder 程序 50
Bibliography / 文献 52
Chapter 4:Comments / 注释 53
Comments Do Not Make Up for Bad Code /注释不能美化糟糕的代码 55
Explain Yourself in Code / 用代码来阐述 55
Good Comments / 好注释 55
Bad Comments / 坏注释 59
Bibliography / 文献 74
Chapter 6:Objects and Data Structures / 对象和数据结构 93
Data Abstraction / 数据抽象 93
Data/Object Anti-Symmetry / 数据、对象的反对称性 95
The Law of Demeter / 得墨忒耳律 97
Data Transfer Objects / 数据传送对象 100
Conclusion / 小结 101
Bibliography / 文献 101
Chapter 7:Error Handling / 错误处理 103
Use Exceptions Rather Than Return Codes / 使用异常而非返回码 104
Write Your Try-Catch-Finally Statement First /
先写try-catch-finally 语句 105
Use Unchecked Exceptions / 使用未检异常 106
Provide Context with Exceptions / 给出异常发生的环境说明 107
Define Exception Classes in Terms of a Caller’s Needs /
依调用者需要定义异常类 107
Define the Normal Flow / 定义常规流程 109
Don’t Return Null / 别返回null 值 110
Don’t Pass Null / 别传递null 值 111
Conclusion / 小结 112
Bibliography / 文献 112
Chapter 8:Boundaries / 边界 113
Using Third-Party Code / 使用第三方代码 114
Exploring and Learning Boundaries / 浏览和学习边界 116
Learning log4j / 学习log4j 116
Learning Tests Are Better Than Free /
学习性测试的好处不只是免费 118
Using Code That Does Not Yet Exist / 使用尚不存在的代码 118
Clean Boundaries / 整洁的边界 120
Bibliography / 文献 120
Chapter 9:Unit Tests / 单元测试 121
The Three Laws of TDD / TDD 三定律 122
Keeping Tests Clean / 保持测试整洁 123
Clean Tests / 整洁的测试 124
One Assert per Test / 每个测试一个断言 130
F.I.R.S.T. 132
Conclusion / 小结 133
Bibliography / 文献 133
Chapter 10:Classes / 类 135
Class Organization / 类的组织 136
Classes Should Be Small! / 类应该短小 136
Organizing for Change / 为了修改而组织 147
Bibliography / 文献 151
Chapter 11:Systems / 系统 153
How Would You Build a City / 如何建造一个城市 154
Separate Constructing a System from Using It /
将系统的构造与使用分开 154
Scaling Up / 扩容 157
Java Proxies / Java 代理 161
Pure Java AOP Frameworks / 纯Java AOP 框架 163
AspectJ Aspects / AspectJ 的方面 166
Test Drive the System Architecture / 测试驱动系统架构 166
Optimize Decision Making / 优化决策 167
Use Standards Wisely, When They Add Demonstrable Value /
明智使用添加了可论证价值的标准 168
Systems Need Domain-Specific Languages /
系统需要领域特定语言 168
Conclusion / 小结 169
Bibliography / 文献 169
Chapter 16:Refactoring SerialDate / 重构SerialDate 267
First, Make It Work / 首先,让它能工作 268
Then Make It Right / 让它做对 270
Conclusion / 小结 284
Bibliography / 文献 284