同一个 class 内的两个或多个方法有重复代码:使用 Extract Method 提抽取复代码。
多个互为兄弟的 class 含重复代码:对这些 class 使用 Extract Method 提抽取复代码,再使用
pull up method 把重复代码向上拉到父类中。
多个不相关的 class 出现重复代码:应该考虑使用 Extract Class,将重复代码抽取到一个独立的
class 中。抽取时应注意这些代码是否真的需要一个独立的类,应照 OO 原则将这些代码归属到
应该拥有这些代码的 class 中。
理清 class 的职责,把一些成员变量和方法抽取到一个新的 class 中或子类中。
技巧:可以先确定客户端如何使用它们,使用 Extract Interface 抽取接口,这可以帮助程序员
看清楚该如何分解这个 class。
参数过多可以考虑把这些参数由一个对象提供。
如果这些参数缺乏合理的对象归属,可以考虑为它们创建一个参数对象。
一般情况下一个变更修改应该只有一个修改点,如果不能做到这点,就应该想想这些地方是否
需要重构了(可以看看 OOAD 原则中的单一责任原则)。
如果需要修改的代码散布四处将会很难找到它们,也很容易忘记某个重要的修改。这种情况下
可以考虑把所有需要修改的代码放进同一个 class。如果没有合适的 class 可以创建一个,比如
说创建一个 Inline class。
当发现一个 class 的某个方法中使用了很多另一个对象的方法或值时就应该考虑把这个方法移到
它该去的地方。
有时候方法中只有一部分代码出现这种情况就应该考虑把这部分代码抽取出成独立的方法再移
到它该去的地方。
如果一个方法用上数个 class 的特性,那么应该考虑把这部份代码抽取为一个个的小方法再把它
们移到它该去的地方。
不管怎么说最根本的原则是总是将一起变化的东西放到一块儿。
应该使用面向对象中的多态还替换 case 语句。
分享到:
相关推荐
Refactoring2-free-chapter_cn
Learn about refactoring and how it relates to CSS Explore methods for executing a CSS refactoring Learn how to deal with browser inconsistencies Investigate common CSS antipatterns—and how to ...
Professional-Refactoring-WorkbookProfessional-Refactoring-Workbook
How do I begin to talk about refactoring? The traditional way is by introducing the history of the subject, broad principles, and the like. When somebody does that at a conference, I get slightly ...
改善既有代码的设计。系统发展到一定阶段后,使用重构的方式,不改变系统的外部功能,只对内部的结构进行重新的整理。通过重构,不断的调整系统的结构,使系统对于需求的变更始终具有较强的适应能力。
a book about SQL refactoring
This book is about the marriage of refactoring-the process of improving the design of existing code-with patterns, the classic solution to design problem.
python库。 资源全名:JoeLiu_RF_Refactoring-1.0.2-py3-none-any.whl
java refactoring : martin ppt
Refactoring-重构_改善既有代码的设计
重构规范 原始规格 问题:出现频率最高的词 ... 每个提交本身就是一个项目,当我们进入最后一个项目时,所有的验收测试、单元测试和实现代码都被充实了。 这是一种说明开发人员将如何开始编写代码的方式。
book-refactoring2-code 存储 《重构: 改善既有代码的设计第二版》的一些示例代码 单元测试 使用 作为我们单元测试套件 运行所有单元测试: npm run test 运行某个单元测试: npx mocha ./src/chapter1/*.test.js 变化...
代码重构(refactoring)是指在保持程序外部表现不变的情况下改变其内部实现的做法。重构的目标主要是改进程序的内部质量,例如增加代码可读性,简化代码结构,增强可维护性、性能或扩展性。即使初期设计得再合理,...
Refactoring-Improving+the+Design+of+Existing+Code.pdf Refactoring-Improving+the+Design+of+Existing+Code.pdf
Refactoring: Improving the Design of Existing Code 重构领域必读书目,江湖地位不必多说,英文原版,高清。
Refactoring-Improving the Design of Existing Code
重构,改善既有代码的设计(中文版,Martin Fowler 著).pdf Addison+Wesley+-+Refactoring-Improving+the+Design+of+Existing+Code.pdf(英文版) 连个pdf ,分布为中,j英版的。
refactoring-classes-ts-ignite
Refactoring to Patterns reflects three years of refinement and the insights of more than sixty software engineering thought leaders in the global patterns, refactoring, and agile development ...