mistake.md - cccnqu/se106a GitHub Wiki
Refactoring (重構)
Why ?
- Requirements Change
- Design need to be improved
- Slopiness / Laziness (Copy & Paste ....)
Typeof Refactoring
https://classroom.udacity.com/courses/ud805/lessons/3608718991/concepts/4421088260923
- Collapse Hierarchy : a. 子類別太像母類別,乾脆合併。
- ex :
- Consolidate Conditional Expression : 把條件式用 boolean 函數表示,意義更清楚。
- ex: summer(date)
- Decompose Conditionals.
- ex: 夏天冬天票價不同 if summer(date) summerPrice() else winterPrice()
- Extract Class : 發現一個類別開始做兩個類別的事情時,乾脆抽出一個新類別,變成兩個。
- ex: Person => Person + Phone Number
- Inline Class : 當發現一個類別沒做甚麼事時,乾脆把feature併到其他類別後刪除之。
- ex: Person + Office => Person (with Office number)
- Extract Method :
- ex: PrintOwing() => PringOwing() { ... printDetails() ... }
- video : https://classroom.udacity.com/courses/ud805/lessons/3608718991/concepts/4404287920923
Risk
- 可能影響其他元件,讓系統失效或產生 bug.
- 在 production 當中,特別是後期已完成 System Test 之後,別輕易重構!
Cost
- Manual Work (修改的代價)
- Test development and maintenance (測試案例可能需要跟著修,還要重新測試,....)
- Documentation Maintenance (文件也要跟著修,還要充新建置出版)
When not to Refactor ?
- When code is broken
- When a deadline is close (交期近了)
- When there is no reason to !
Bed Smealls
- Duplicated code => extract method
- Long method => extract method, decompose conditional, ...
- Large class => extract class (or subclass)
- Long parameter list
- Divergent change
- Shotgun supgery => move method/field, inline class
- 現象:每次改了一個地方,就得連動改很多其他物件的程式碼
- Feature envy => extract method, move method
- 現象: 每次改某物件就得改另一物件。
- Data clumps
- Primitive Obs...?
- Switch Statements
- Parallel interface hierarchy
- Lazy class
- Speculative generality
- Temporary field
- Message Chains
- Middle man
- Inappropriate intimacy
- Incomplete library class
- Data class
- Refused ...