More Related Content
Similar to 重構—改善既有程式的設計(chapter 10) (19)
More from Chris Huang (20)
重構—改善既有程式的設計(chapter 10)
- 2. Agenda Function Name Function Parameter Private Function Constructor Exception private example(intparam) Classification 5/26/2011 2
- 4. Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 4
- 10. Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 10
- 11. Add Parameter Motivation 修改函式後, 需要增加資訊 壞味道 :Data Clumps (P81) 建議:Introduce Parameter Object (295) Classification 5/26/2011 11
- 12. Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 12
- 14. Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 14
- 15. Separate Query from Modifier Motivation 某個函式既回傳物件狀態值, 又修改物件狀態 (getXXX + setXXX) 既有返回值又有副作用就應該分離 (getXXX only) Meyer’s Rule: 任何有返回值的函式皆不應有副作用 優點: 增加重複查詢的效能 總是獲得相同的結果 [Allen] 函式名稱與期望結果一致 Example Classification 5/26/2011 15
- 18. Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 18
- 19. Parameterize Method Motivation 問題: 某個函式做類似的工作, 但是函式本體卻包含不同的值 方式 使用單一函式, 並以參數來表達不同的值 將少量數值視為參數, 找出重覆的程式碼 優點: 減少重複的程式碼 [Allen] 與 Replace Parameter with Explicit Method 相反 Example Classification 5/26/2011 19
- 22. Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 22
- 23. Replace Parameter with Explicit Methods Motivation 與Parameterize Method 相反 條件: 離散取值 函式中以條件事檢查參數 [Allen] 將 IF-ELSE 或 SWITCH 移除 [Allen] Code 不一樣 優點: 避免出現條件式 利用編譯器檢查程式 介面清楚 使用參數時, 則須判斷參數合法性 [Allen] 如 switch之default Example Classification 5/26/2011 23
- 25. Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 25
- 26. Preserve Whole Object Motivation 參數為某一物件中取出, 則將該物件當作輸入參數 優點: 避免新增修改參數項 減少參數數量, 方便其他程式呼叫使用 條件: 因依存關係導致結構惡化則不可使用 Example Classification 5/26/2011 26
- 29. Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 29
- 30. Replace Parameter with Methods Motivation 物件換起某個函式, 並將所得結果作為參數, 傳遞給另一個函式 接受該參數的函式也可以喚起前一個函式 條件: 接收端是否可以透過計算而取得函數值 優點: 減少參數數量 使函式容易理解 Example Classification 5/26/2011 30
- 33. Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 33
- 34. Introduce Parameter Object Motivation 一組參數一起被傳遞 將一組參數組織再一起 [Allen] 解決 Add Parameter 的問題 優點: 減少參數量 找出部分程式碼可移到輸入的類別中 Example Classification 5/26/2011 34
- 42. Remove Setting Method Motivation 物件中某欄位, 應該在初創時被設置, 然後不再改變 如果不希望再被改變, 則直接不提供 setXXX [Allen] 專用於Final 的變數 Example Classification 5/26/2011 42
- 44. Replace Constructor with Factory Method Motivation 當需要使用 Type Code 創建建構式 使用Factory Method實做建構式 [Allen] 依照不同Type有不同行為時 Example * 3 Classification 5/26/2011 44
- 49. Replace Error Code with Exception Motivation [Allen] 與Replace Error Code with Exception相反 優點: 將異常與一般程式分開 Example Classification 5/26/2011 52 平行
- 52. Replace Exception with Test Motivation [Allen] 與Replace Error Code with Exception相反 避免Exception濫用 呼叫函式前先檢查必要條件 Example Classification 5/26/2011 55