文章目录
  1. 1. Definition
  2. 2. Bad Smells
    1. 2.1. Duplicated Code
    2. 2.2. Long Method
    3. 2.3. Large Class
    4. 2.4. Long Parameter List
    5. 2.5. Divergent Change
    6. 2.6. Shotgun Surgery
    7. 2.7. Feature Envy
    8. 2.8. Data Clumps
    9. 2.9. Primitive Obsession
    10. 2.10. Switch Statements
    11. 2.11. Parallel Inheritance Hierarchies
    12. 2.12. Lazy Class
    13. 2.13. Speculative Generality
    14. 2.14. Temporary Field
    15. 2.15. Message Chains
    16. 2.16. Middle Man
    17. 2.17. Inappropriate Intimacy
    18. 2.18. Alternative Classes with Different Interfaces
    19. 2.19. Incomplete Library Class
    20. 2.20. Data Class
    21. 2.21. Refused Bequest
    22. 2.22. Comments

最近做了个简单的项目:调用已有接口,然后组装数据返回给客户端,本身功能很简单,没有太多技术含量,但是这次稍微学习了一下代码重构。同事在review我的代码的时候,给出了很多重构建议,然后发现自己以前的代码设计中存在很多问题,所以就决定阅读一下Martin Fowler的名作《Refactoring:Improving the Design of Existing Code》。

简单学习了前两章之后,我决定对后面对章节做个简单读书笔记。第一篇就是原书的第三章-Bad Smells in Code.

Definition

Refactoring (noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.

Refactor (verb): to restructure software by applying a series of refactorings without changing its observable behavior.

Bad Smells

Duplicated Code

  1. Extract Method
  2. Pull Up Field
  3. Form Template Method
  4. Substitute Algorithm
  5. Extract Class

Long Method

  1. Extract Method
  2. Replace Temp with Query
  3. Introduce Parameter Object
  4. Preserve Whole Object
  5. Replace Method with Method Object
  6. Decpmpose Conditional

Large Class

  1. Extract Class
  2. Extract Subclass
  3. Extract Interface
  4. Duplicate Observed Data

Long Parameter List

  1. Replace Parameter with Method
  2. Preserve Whole Object
  3. Introduce Parameter Object

Divergent Change

  1. Extract Class

Shotgun Surgery

  1. Move Method
  2. Move Field
  3. Inline Class

Feature Envy

  1. Extract Method
  2. Move Method

Data Clumps

  1. Extract Class
  2. Introduce Parameter Object
  3. Preserve Whole Object

Primitive Obsession

  1. Replace Data Value with Object
  2. Replace Type Code with Class
  3. Replace Type Code with Subclasses
  4. Replace Type Code with State/Strategy
  5. Introdce Parameter Object
  6. Replace Array with Object

Switch Statements

  1. Extract Method
  2. Move Method
  3. Replace Type Code with Subclasses
  4. Replace Type Code with State/Strategy
  5. Replace Conditional with Polymorphism
  6. Replace Parameter with Explicit Methods
  7. Introduce Null Object

Parallel Inheritance Hierarchies

  1. Move Method
  2. Move Field

Lazy Class

  1. Collapse Hierarchy
  2. Inline Class

Speculative Generality

  1. Collapse Hierarchy
  2. Inline Class
  3. Remove Parameter
  4. Rename Method

Temporary Field

  1. Extract Class
  2. Introduce Null Object

Message Chains

  1. Hide Delegate
  2. Extract Method
  3. Move Mehtod

Middle Man

  1. Remove Middle Man
  2. Inline Method
  3. Replace Delegation with Inheritance

Inappropriate Intimacy

  1. Move Method
  2. Move Field
  3. Change Bidirectional Association to Unidirectional
  4. Extact Class
  5. Hide Delegate
  6. Replace Delegation with Inheritance

Alternative Classes with Different Interfaces

  1. Rename Method
  2. Move Method
  3. Extract Superclass

Incomplete Library Class

  1. Introduce Foreign Method
  2. Introduce Local Extension

Data Class

  1. Encapsulate Field
  2. Encapsulate Collection
  3. Remove Setting Method
  4. Move Method
  5. Extract Method
  6. Hide Method

Refused Bequest

  1. Replace Inheritance with Delegation

Comments

  1. Extract Method
  2. Rename Method
  3. Introduce Assertion
文章目录
  1. 1. Definition
  2. 2. Bad Smells
    1. 2.1. Duplicated Code
    2. 2.2. Long Method
    3. 2.3. Large Class
    4. 2.4. Long Parameter List
    5. 2.5. Divergent Change
    6. 2.6. Shotgun Surgery
    7. 2.7. Feature Envy
    8. 2.8. Data Clumps
    9. 2.9. Primitive Obsession
    10. 2.10. Switch Statements
    11. 2.11. Parallel Inheritance Hierarchies
    12. 2.12. Lazy Class
    13. 2.13. Speculative Generality
    14. 2.14. Temporary Field
    15. 2.15. Message Chains
    16. 2.16. Middle Man
    17. 2.17. Inappropriate Intimacy
    18. 2.18. Alternative Classes with Different Interfaces
    19. 2.19. Incomplete Library Class
    20. 2.20. Data Class
    21. 2.21. Refused Bequest
    22. 2.22. Comments