【重构】二、重构列表和重构方法速查表
重构和设计模式在线快查:Design Patterns & Refactoring
坏味道
分类 | Bad code smells |
---|---|
BloatersBloaters are code, methods and classes that have increased to such gargantuan proportions that they are hard to work with. Usually these smells do not crop up right away, rather they accumulate over time as the program evolves (and especially when nobody makes an effort to eradicate them). | 过长方法 |
过长参数列表 | |
过大类 | |
数据泥团 | |
基本类型偏执 | |
Object-Orientation AbusersAll these smells are incomplete or incorrect application of object-oriented programming principles. | switch语句 |
被拒绝的遗赠 | |
临时字段 | |
Alternative Classes with Different Interfaces 异曲同工的类 | |
Change PreventersThese smells mean that if you need to change something in one place in your code, you have to make many changes in other places too. Program development becomes much more complicated and expensive as a result. | 发散式变化 |
霰弹式修改 | |
Parallel Inheritance Hierarchies 平行继承 | |
DispensablesA dispensable is something pointless and unneeded whose absence would make the code cleaner, more efficient and easier to understand. | 注释 |
重复代码 | |
冗余类 | |
纯数据类 | |
死码 | |
夸夸其谈未来性 | |
CouplersAll the smells in this group contribute to excessive coupling between classes or show what happens if coupling is replaced by excessive delegation. | 依恋情结 |
过度亲密 | |
消息链 | |
中间人 | |
不完整的类库 |
重构手段
分类 | Refactoring techniques |
---|---|
Composing methodsMuch of refactoring is devoted to correctly composing methods. In most cases, excessively long methods are the root of all evil. The vagaries of code inside these methods conceal the execution logic and make the method extremely hard to understand – and even harder to change.
The refactoring techniques in this group streamline methods, remove code duplication, and pave the way for future improvements. | 提炼函数 |
内联函数 | |
提炼变量 | |
内联变量 | |
以查询替代临时变量 | |
拆分临时变量 | |
Remove Assignments to Parameters 移除对参数的赋值操作 | |
Replace Method with Method Object 用方法对象替换方法 | |
替换算法 | |
Moving Features between ObjectsEven if you have distributed functionality among different classes in a less-than-perfect way, there is still hope.
These refactoring techniques show how to safely move functionality between classes, create new classes, and hide implementation details from public access. | 移动方法 |
移动字段 | |
内联类 | |
隐藏委托关系 | |
移除中间人 | |
引入外加方法 | |
引入本地扩展 | |
Organizing DataThese refactoring techniques help with data handling, replacing primitives with rich class functionality. Another important result is untangling of class associations, which makes classes more portable and reusable.
| |
Replace Data Value with Object 以对象替换数据值 | |
将值对象改为引用对象 | |
将引用对象改为值对象 | |
用对象替换数组 | |
重复被观察数据 | |
Replace Magic Number with Symbolic Constant 用符号常量代替魔法值 | |
封装字段 | |
封装集合 | |
类型代码替换为类 | |
Replace Type Code with Subclasses 类型代码替换为子类 | |
Replace Type Code with State/Strategy 类型代码替换为状态/策略模式 | |
子类替换为字段 | |
Simplifying Conditional ExpressionsConditionals tend to get more and more complicated in their logic over time, and there are yet more techniques to combat this as well. | 分解条件表达式 |
移除控制标志 | |
Replace Nested Conditional with Guard Clauses 以卫语句取代条件表达式 | |
Replace Conditional with Polymorphism 以多态替换条件表达式 | |
引入空对象 | |
引入断言 | |
Simplifying Method CallsThese techniques make method calls simpler and easier to understand. This, in turn, simplifies the interfaces for interaction between classes. | 重命名方法 |
添加参数 | |
移除参数 | |
查询和更改分开 | |
参数化方法 | |
保持对象完整 | |
Replace Parameter with Method Call 以方法调用替换参数 | |
引入参数对象 | |
移除设值函数 | |
隐藏方法 | |
Replace Constructor with Factory Method 用工厂方法替换构造方法 | |
Replace Error Code with Exception 用异常替换失败码 | |
用条件判断替换异常 | |
字段上移 | |
方法上移 | |
构造函数体上移 | |
方法下移 | |
字段下移 | |
提取子类 | |
提取超类 | |
提取接口 | |
折叠继承关系 | |
塑造模板方法 | |
Replace Inheritance with Delegation 用委托替代继承 | |
Replace Delegation with Inheritance 用继承替代委托 |