5

代码味道及重构手段

 3 years ago
source link: https://blog.csdn.net/LoveLion/article/details/9889095
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

代码味道及重构手段

LoveLion 2013-08-11 09:29:11 6497

       Martin Fowler针对22种代码味道,提出了相应的重构手段,微笑。具体而言,如下表所示:

代码味道

常用重构手段

接口不同的相似类(Alternative Classes with Different Interfaces)【异曲同工的类】

● 重命名方法(Rename Method)

● 搬移方法(Move Method)

注释(Comments)【过多的注释】

● 抽取方法(Extract Method)

● 引入断言(Introduce Assertion)

数据类(Data Class)【纯稚的数据类】

● 搬移方法(Move Method)

● 封装字段(Encapsulate Field)

● 封装集合(Encapsulate Collection)

数据泥团(Data Clump)

● 抽取类(Extract Class)

● 引入参数对象(Introduce Parameter Object)

● 保持对象完整(Preserve Whole Object)

发散式改变(Divergent Change)

● 抽取类(Extract Class)

重复性代码(Duplicated Code)

● 抽取方法(Extract Method)

● 抽取类(Extract Class)

● 上移方法(Pull Up Method)

● 构建模板方法(Form Template Method)

依恋情结(Feature Envy)

● 搬移方法(Move Method)

● 搬移字段(Move Field)

● 抽取方法(Extract Method)

不当的紧密性(Inappropriate Intimacy)【狎昵关系】

● 搬移方法(Move Method)

● 搬移字段(Move Field)

● 将双向关联改为单向(Change Bidirectional Association to Unidirectional)

● 将继承替换成委托(Replace Inheritance with Delegation)

● 隐藏委托(Hide Delegate)

不完备的库类(Incomplete Library Class)

● 引入外来方法(Introduce Foreign Method)、

● 引入本地扩展(Introduce Local Extension)

过大的类(Large Class)

● 抽取类(Extract Class)

● 抽取子类(Extract Subclass)

● 抽取接口(Extract Interface)

● 将数据值替换为对象(Replace Data Value with Object)

懒惰类(Lazy Class)【冗赘类】

● 内联类(Inline Class)

● 折叠继承体系(Collapse Hierarchy)

过长的方法(Long Method)

● 抽取方法(Extract Method)

● 将临时变量替换为查询(Replace Temp with Query)

● 将方法替换为方法对象(Replace Method with Method Object)

● 分解条件式(Decompose Conditional)

过长的参数表(Long Parameter List)

● 将参数替换为方法(Replace Parameter with Method)

● 引入参数对象(Introduce Parameter Object)

● 保持对象完整(Preserve Whole Object)

消息链(Message Chains)【过渡耦合的消息链】

● 隐藏委托(Hide Delegate)

中间人(Middle Man)

● 移除中间人(Remove Middle Man)

● 内联方法(Inline Method)

● 将委托替换为继承(Replace Delegation with Inheritance)

并行继承体系(Parallel Inheritance Hierarchies)

● 搬移方法(Move Method)

● 搬移字段(Move Field)

基本类型困扰(Primitive Obsession)【基本类型偏执】

● 将数据值替换为对象(Replace Data Value with Object)

● 抽取类(Extract Class)

● 引入参数对象(Introduce Parameter Object)

● 将数组替换为对象(Replace Array with Object)

● 将类型码替换为类(Replace Type Code with Class)

● 将类型码替换为子类(Replace Type Code with Subclasses)

● 将类型码替换为状态/策略(Replace Type Code with State/Strategy)

拒收的遗赠(Refused Bequest)

● 将继承替换为委托(Replace Inheritance with Delegation)

霰弹式修改(Shotgun Surgery)

● 搬移方法(Move Method)

● 搬移字段(Move Field)

● 内联类(Inline Class)

过分一般性(Speculative Generality)【夸夸其谈的未来性】

● 折叠继承体系(Collapse Hierarchy)

● 内联类(Inline Class)

● 移除参数(Remove Parameter)

● 重命名方法(Rename Method)

Switch语句(Switch Statements)【Switch惊悚现身】

● 将条件式替换为多态(Replace Conditional with Polymorphism)

● 将类型码替换为子类(Replace Type Code with Subclasses)

● 将类型码替换为状态/策略(Replace Type Code with State/Strategy)

● 将参数替换为显式方法(Replace Parameter with Explicit Methods)

● 引入Null对象(Introduce Null Object)

临时字段(Temporary Field)【令人迷惑的暂时值域】

● 抽取类(Extract Class)

● 引入Null对象(Introduce Null Object)

       参考资料:

 【作者:刘伟 http://blog.csdn.net/lovelion


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK