第1章:重构,第一个案例(Refactoring, a First Example)1
1.1起点2
1.2重构的第一步7
1.3分解并重组(Statement) 8
1.4运用多态(polymorphism)取代与价格相关的条件逻辑34
1.5结语52
第2章:重构原则(Principles in Refactoring)53
2.1何谓重构?53
2.2为何重构?55
2.3何时重构?57
2.4怎么对经理说?60
2.5重构的难题62
2.6重构与设计66
2.7重构与性能(Performance)69
2.8重构起源何处?71
第3章:代码的坏味道(Bad Smells in Code, by Kent Beck and Martin Fowler)75
3.1 Duplicated Code(重复的代码)76
3.2 Long Method(过长函数)76
3.3 Large Class(过大类)78
3.4 Long Parameter List(过长参数列)78
3.5 Divergent Change(发散式变化)79
3.6 Shortgun Surgery(霰弹式修改)80
3.7 Feature Envy(依恋情结)80
3.8 Data Clumps(数据泥团)81
3.9 Primitive Obsession(基本型别偏执)81
3.10 Switch Statements(switch惊悚现身)82
3.11 Parallel Inheritance Hierarchies(平行继承体系)83
3.12 Lazy Class(冗赘类)83
3.13 Speculative Generality(夸夸其谈未来性)83
3.14 Temporary Field(令人迷惑的暂时值域)84
3.15 Message Chains(过度耦合的消息链)84
3.16 Middle Man(中间转手人)85
3.17 Inappropriate Intimacy(狎昵关系)85
3.18 Alternative Classes with Different Interfaces(异曲同工的类)85
3.19 Incomplete Library Class(不完善的程序库类)86
3.20 Data Class(纯稚的数据类)86
3.21 Refused Bequest(被拒绝的遗赠)87
3.22 Comments(过多的注释)87
第4章:建立测试体系(Building Tests)89
4.1自我测试码(Self-testing Code)的价值89
4.2 JUnit测试框架(Testing Framework)91
4.3添加更多测试97
第5章:重构名录(Toward a Catalog of Refactoring)103
5.1重构的记录格式(Format of Refactorings)103
5.2寻找引用点(Finding References)105
5.3这些重构准则有多成熟?106
第6章:重新组织你的函数(Composing Methods)109
6.1 Extract Method(提炼函数)110
6.2 Inline Method(将函数内联化)117
6.3 Inline Temp(将临时变量内联化)119
6.4 Replace Temp With Query(以查询取代临时变量)120
6.5 Introduce Explaining Variable(引入解释性变量)124
6.6 Split Temporary Variable(剖解临时变量)128
6.7 Remove Assignments to Parameters(移除对参数的赋值动作)131
6.8 Replace Method with Method Object(以函数对象取代函数)135
6.9 Substitute Algorithm(替换你的算法)139
第7章:在对象之间移动特性(Moving Features Between Objects)141
7.1 Move Method(搬移函数)142
7.2 Move Field(搬移值域)146
7.3 Extract Class(提炼类)149
7.4 Inline Class(将类内联化)154
7.5 Hide Delegate(隐藏「委托关系」)157
7.6 Remove Middle Man(移除中间人)160
7.7 Introduce Foreign Method(引入外加函数)162
7.8 Introduce Local Extension(引入本地扩展)164
第8章:重新组织你的数据(Organizing Data)169
8.1 Self Encapsulate Field(自封装值域)171
8.2 Replace Data Value with Object(以对象取代数据值)175
8.3 Change Value to Reference(将实值对象改为引用对象)179
8.4 Change Reference to Value(将引用对象改为实值对象)183
8.5 Replace Array with Object(以对象取代数组)186
8.6 Duplicate Observed Data(复制「被监视数据」)189
8.7 Change Unidirectional Association to Bidirectional 197
(将单向关联改为双向)
8.8 Change Bidirectional Association to Unidirectional 200
(将双向关联改为单向)
8.9 Replace Magic Number with Symbolic Constant 204
(以符号常量/字面常量 取代魔法数)
8.10 Encapsulate Field(封装值域)206
8.11 Encapsulate Collection(封装群集)208
8.12 Replace Record with Data Class(以数据类取代记录)217
8.13 Replace Type Code with Class(以类取代型别码)218
8.14 Replace Type Code with Subclasses 223
(以子类取代型别码)
8.15 Replace Type Code with State/Strategy 227
(以State/Strategy取代型别码)
8.16 Replace Subclass with Fields(以值域取代子类)232
第9章:简化条件表达式(Simplifying Conditional Expressions)237
9.1 Decompose Conditional(分解条件式)238
9.2 Consolidate Conditional Expression(合并条件式)240
9.3 Consolidate Duplicate Conditional Fragments 243
(合并重复的条件片段)
9.4 Remove Control Flag(移除控制标记)245
9.5 Replace Nested Conditional with Guard Clauses 250
(以卫语句取代嵌套条件式)
9.6 Replace Conditional with Polymorphism(以多态取代条件式)255
9.7 Introduce Null Object(引入Null对象)260
9.8 Introduce Assertion(引入断言)267
第10章:简化函数呼叫(Making Method Calls Simpler)271
10.1 Rename Method(重新命名函数)273
10.2 Add Parameter(添加参数)275
10.3 Remove Parameter(移除参数)277
10.4 Separate Query from Modifier(将查询函数和修改函数分离)279
10.5 Parameterize Method(令函数携带参数)283
10.6 Replace Parameter with Explicit Methods(以明确函数取代参数)285
10.7 Preserve Whole Object(保持对象完整)288
10.8 Replace Parameter with Method(以函数取代参数)292
10.9 Introduce Parameter Object(引入参数对象)295
10.10 Remove Setting Method(移除设值函数)300
10.11 Hide Method(隐藏你的函数)303
10.12 Replace Constructor with Factory Method(以工厂方法取代构造函数)304
10.13 Encapsulate Downcast(封装「向下转型」动作)308
10.14 Replace Error Code with Exception(以异常取代错误码)310
10.15 Replace Exception with Test(以测试取代异常)315
第11章:处理概括关系(Dealing with Generalization)319
11.1 Pull Up Field(值域上移)320
11.2 Pull Up Method(函数上移)322
11.3 Pull Up Constructor Body(构造函数本体上移)325
11.4 Push Down Method(函数下移)328
11.5 Push Down Field(值域下移)329
11.6 Extract Subclass(提炼子类)330
11.7 Extract Superclass(提炼超类)336
11.8 Extract Interface(提炼接口)341
11.9 Collapse Hierarchy(折叠继承体系)344
11.10 Form Template Method(塑造模板函数)345
11.11 Replace Inheritance with Delegation(以委托取代继承)352
11.12 Replace Delegation with Inheritance(以继承取代委托)355
第12章:大型重构(Big Refactorings, by Kent Beck and Martin Fowler)359
12.1 Tease Apart Inheritance(疏理并分解继承体系)362
12.2 Convert Procedural Design to Objects368
(将过程化设计转化为对象设计)
12.3 Separate Domain from Presentation(将领域和表述/显示分离)370
12.4 Extract Hierarchy(提炼继承体系)375
第13章:重构, 复用, 与现实379
(Refactoring, Reuse, and Reality, by William Opdyke)
13.1现实的检验380
13.2为什么开发者不愿意重构他们的程序?381
13.3现实的检验(再论)394
13.4重构的资源和参考数据394
13.5从重构联想到软件复用和技术传播395
13.6结语397
13.7参考文献397
第14章:重构工具(Refactoring Tools, by Don Roberts and John Brant)401
14.1使用工具进行重构401
14.2重构工具的技术标准(Technical Criteria)403
14.3重构工具的实用标准(Practical Criteria)405
14.4小结407
第15章:集成(Put It All Together, by Kent Beck)409
参考书目(References)413
原音重现(List of Soundbites)417
索引419