【重構】二、重構列表和重構方法速查表
重構和設計模式在線快查: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 用繼承替代委託 |