Classification 5/26/2011<br />1<br />Refactoring10 – Making Method Calls Simpler<br />Allen Chien<br />2011-05-15<br />
Agenda<br />Function Name<br />Function Parameter<br />Private Function<br />Constructor<br />Exception<br />private examp...
Function Name<br />Rename Method<br />Classification 5/26/2011<br />3<br />
Function Parameters<br />Add Parameters<br />Remove Parameters<br />Separate Query From Modifier<br />Parameterize Method<...
Private Function<br />Hide Method<br />Classification 5/26/2011<br />5<br />
Constructor<br />Remove Setting Method<br />Replace Constructor with Factory Method<br />Encapsulate Downcast<br />Classif...
Exception<br />Replace Error Code with Exception<br />Replace Exception with Test<br />Classification 5/26/2011<br />7<br />
Function Name<br />Rename Method<br />(忽略  “做法”)<br />Classification 5/26/2011<br />8<br />
Rename Method<br />Motivation<br />函式名稱應該準確表達它的用途<br />為函式寫上一句註釋, 然後為該註釋給予一個名稱<br />Example<br />Classification 5/26/2011<...
Function Parameters<br />Add Parameters<br />Remove Parameters<br />Separate Query From Modifier<br />Parameterize Method<...
Add Parameter<br />Motivation<br />修改函式後, 需要增加資訊<br />壞味道 :Data Clumps (P81)<br />建議:Introduce Parameter Object (295)<br /...
Function Parameters<br />Add Parameters<br />Remove Parameters<br />Separate Query From Modifier<br />Parameterize Method<...
Remove Parameter<br />Motivation<br />修改函式後, 去除參數的重構<br />在多型的情況下, 需要檢查該函式是否已被其他程式實做<br />Classification 5/26/2011<br />13...
Function Parameters<br />Add Parameters<br />Remove Parameters<br />Separate Query From Modifier<br />Parameterize Method<...
Separate Query from Modifier<br />Motivation<br />某個函式既回傳物件狀態值, 又修改物件狀態 (getXXX + setXXX)<br />既有返回值又有副作用就應該分離 (getXXX onl...
Classification 5/26/2011<br />16<br />
Classification 5/26/2011<br />17<br />Substitute Algorithm (139)<br />
Function Parameters<br />Add Parameters<br />Remove Parameters<br />Separate Query From Modifier<br />Parameterize Method<...
Parameterize Method<br />Motivation<br />問題:  某個函式做類似的工作, 但是函式本體卻包含不同的值<br />方式<br />使用單一函式, 並以參數來表達不同的值<br />將少量數值視為參數, 找...
Classification 5/26/2011<br />20<br />
Classification 5/26/2011<br />21<br />
Function Parameters<br />Add Parameters<br />Remove Parameters<br />Separate Query From Modifier<br />Parameterize Method<...
Replace Parameter with Explicit Methods<br />Motivation<br />與Parameterize Method 相反<br />條件:<br />離散取值<br />函式中以條件事檢查參數<b...
Classification 5/26/2011<br />24<br />
Function Parameters<br />Add Parameters<br />Remove Parameters<br />Separate Query From Modifier<br />Parameterize Method<...
Preserve Whole Object<br />Motivation<br />參數為某一物件中取出, 則將該物件當作輸入參數<br />優點:<br />避免新增修改參數項<br />減少參數數量, 方便其他程式呼叫使用<br />條件...
Classification 5/26/2011<br />27<br />
Classification 5/26/2011<br />28<br />
Function Parameters<br />Add Parameters<br />Remove Parameters<br />Separate Query From Modifier<br />Parameterize Method<...
Replace Parameter with Methods<br />Motivation<br />物件換起某個函式, 並將所得結果作為參數, 傳遞給另一個函式<br />接受該參數的函式也可以喚起前一個函式<br />條件:<br />接...
Classification 5/26/2011<br />31<br />
Classification 5/26/2011<br />32<br />Inline Method (117)<br />
Function Parameters<br />Add Parameters<br />Remove Parameters<br />Separate Query From Modifier<br />Parameterize Method<...
Introduce Parameter Object<br />Motivation<br />一組參數一起被傳遞<br />將一組參數組織再一起<br />[Allen] 解決 Add Parameter 的問題<br />優點:<br />...
Classification 5/26/2011<br />35<br />
Classification 5/26/2011<br />36<br />
Classification 5/26/2011<br />37<br />
Classification 5/26/2011<br />38<br />[Allen]  直接存取物件內的資料<br />_start<br />_end<br />
Private Function<br />Remove Setting Method<br />Hide Method<br />Classification 5/26/2011<br />39<br />
Hide Method<br />Motivation<br />類別中某一函式從來沒有被其他類別使用<br />利用IDE工具找出是否有被外部程式Reference<br />若沒有則將其設為Private function<br />
Constructor<br />Remove Setting Method<br />Replace Constructor with Factory Method<br />Encapsulate Downcast<br />Classif...
Remove Setting Method<br />Motivation<br />物件中某欄位, 應該在初創時被設置, 然後不再改變<br />如果不希望再被改變, 則直接不提供 setXXX<br />[Allen]  專用於Final ...
Constructor<br />Remove Setting Method<br />Replace Constructor with Factory Method<br />Encapsulate Downcast<br />Classif...
Replace Constructor with Factory Method<br />Motivation<br />當需要使用 Type Code 創建建構式<br />使用Factory Method實做建構式<br />[Allen]...
Classification 5/26/2011<br />45<br /><ul><li>Example1: 根據整數建構</li></li></ul><li>Classification 5/26/2011<br />46<br /><ul...
Encapsulate Downcast<br />Motivation<br />優點:<br />將轉型動作封裝<br />[Allen] 避免外部程式自行轉型<br />Example<br />Classification 5/26/2...
Classification 5/26/2011<br />50<br />
Exception<br />Replace Error Code with Exception<br />Replace Exception with Test<br />Classification 5/26/2011<br />51<br />
Replace Error Code with Exception<br />Motivation<br />[Allen] 與Replace Error Code with Exception相反<br />優點:<br />將異常與一般程式...
Classification 5/26/2011<br />53<br />說明清楚<br />
Overview<br />Exception<br />Replace Error Code with Exception<br />Replace Exception with Test<br />Classification 5/26/2...
Replace Exception with Test<br />Motivation<br />[Allen] 與Replace Error Code with Exception相反<br />避免Exception濫用<br />呼叫函式...
Classification 5/26/2011<br />56<br />
Classification 5/26/2011<br />57<br />
Classification 5/26/2011<br />58<br />
Summary<br />Classification 5/26/2011<br />59<br />
Thank  YOU !<br />Classification 5/26/2011<br />60<br />
重構—改善既有程式的設計(chapter 10)
重構—改善既有程式的設計(chapter 10)
重構—改善既有程式的設計(chapter 10)
Upcoming SlideShare
Loading in …5
×

重構—改善既有程式的設計(chapter 10)

1,218 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,218
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
65
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

重構—改善既有程式的設計(chapter 10)

  1. 1. Classification 5/26/2011<br />1<br />Refactoring10 – Making Method Calls Simpler<br />Allen Chien<br />2011-05-15<br />
  2. 2. Agenda<br />Function Name<br />Function Parameter<br />Private Function<br />Constructor<br />Exception<br />private example(intparam)<br />Classification 5/26/2011<br />2<br />
  3. 3. Function Name<br />Rename Method<br />Classification 5/26/2011<br />3<br />
  4. 4. Function Parameters<br />Add Parameters<br />Remove Parameters<br />Separate Query From Modifier<br />Parameterize Method<br />Replace Parameter with Explicit Method<br />Preserve Whole Object<br />Replace Parameter with Method<br />Introduce Parameter Object<br />Classification 5/26/2011<br />4<br />
  5. 5. Private Function<br />Hide Method<br />Classification 5/26/2011<br />5<br />
  6. 6. Constructor<br />Remove Setting Method<br />Replace Constructor with Factory Method<br />Encapsulate Downcast<br />Classification 5/26/2011<br />6<br />
  7. 7. Exception<br />Replace Error Code with Exception<br />Replace Exception with Test<br />Classification 5/26/2011<br />7<br />
  8. 8. Function Name<br />Rename Method<br />(忽略 “做法”)<br />Classification 5/26/2011<br />8<br />
  9. 9. Rename Method<br />Motivation<br />函式名稱應該準確表達它的用途<br />為函式寫上一句註釋, 然後為該註釋給予一個名稱<br />Example<br />Classification 5/26/2011<br />9<br />
  10. 10. Function Parameters<br />Add Parameters<br />Remove Parameters<br />Separate Query From Modifier<br />Parameterize Method<br />Replace Parameter with Explicit Method<br />Preserve Whole Object<br />Replace Parameter with Method<br />Introduce Parameter Object<br />Classification 5/26/2011<br />10<br />
  11. 11. Add Parameter<br />Motivation<br />修改函式後, 需要增加資訊<br />壞味道 :Data Clumps (P81)<br />建議:Introduce Parameter Object (295)<br />Classification 5/26/2011<br />11<br />
  12. 12. Function Parameters<br />Add Parameters<br />Remove Parameters<br />Separate Query From Modifier<br />Parameterize Method<br />Replace Parameter with Explicit Method<br />Preserve Whole Object<br />Replace Parameter with Method<br />Introduce Parameter Object<br />Classification 5/26/2011<br />12<br />
  13. 13. Remove Parameter<br />Motivation<br />修改函式後, 去除參數的重構<br />在多型的情況下, 需要檢查該函式是否已被其他程式實做<br />Classification 5/26/2011<br />13<br />
  14. 14. Function Parameters<br />Add Parameters<br />Remove Parameters<br />Separate Query From Modifier<br />Parameterize Method<br />Replace Parameter with Explicit Method<br />Preserve Whole Object<br />Replace Parameter with Method<br />Introduce Parameter Object<br />Classification 5/26/2011<br />14<br />
  15. 15. Separate Query from Modifier<br />Motivation<br />某個函式既回傳物件狀態值, 又修改物件狀態 (getXXX + setXXX)<br />既有返回值又有副作用就應該分離 (getXXX only)<br />Meyer’s Rule: 任何有返回值的函式皆不應有副作用<br />優點:<br />增加重複查詢的效能<br />總是獲得相同的結果<br />[Allen] 函式名稱與期望結果一致<br />Example<br />Classification 5/26/2011<br />15<br />
  16. 16. Classification 5/26/2011<br />16<br />
  17. 17. Classification 5/26/2011<br />17<br />Substitute Algorithm (139)<br />
  18. 18. Function Parameters<br />Add Parameters<br />Remove Parameters<br />Separate Query From Modifier<br />Parameterize Method<br />Replace Parameter with Explicit Method<br />Preserve Whole Object<br />Replace Parameter with Method<br />Introduce Parameter Object<br />Classification 5/26/2011<br />18<br />
  19. 19. Parameterize Method<br />Motivation<br />問題: 某個函式做類似的工作, 但是函式本體卻包含不同的值<br />方式<br />使用單一函式, 並以參數來表達不同的值<br />將少量數值視為參數, 找出重覆的程式碼<br />優點: 減少重複的程式碼<br />[Allen] 與 Replace Parameter with Explicit Method 相反<br />Example<br />Classification 5/26/2011<br />19<br />
  20. 20. Classification 5/26/2011<br />20<br />
  21. 21. Classification 5/26/2011<br />21<br />
  22. 22. Function Parameters<br />Add Parameters<br />Remove Parameters<br />Separate Query From Modifier<br />Parameterize Method<br />Replace Parameter with Explicit Method<br />Preserve Whole Object<br />Replace Parameter with Method<br />Introduce Parameter Object<br />Classification 5/26/2011<br />22<br />
  23. 23. Replace Parameter with Explicit Methods<br />Motivation<br />與Parameterize Method 相反<br />條件:<br />離散取值<br />函式中以條件事檢查參數<br />[Allen] 將 IF-ELSE 或 SWITCH 移除<br />[Allen] Code 不一樣<br />優點:<br />避免出現條件式<br />利用編譯器檢查程式<br />介面清楚<br />使用參數時, 則須判斷參數合法性<br />[Allen] 如 switch之default<br />Example<br />Classification 5/26/2011<br />23<br />
  24. 24. Classification 5/26/2011<br />24<br />
  25. 25. Function Parameters<br />Add Parameters<br />Remove Parameters<br />Separate Query From Modifier<br />Parameterize Method<br />Replace Parameter with Explicit Method<br />Preserve Whole Object<br />Replace Parameter with Method<br />Introduce Parameter Object<br />Classification 5/26/2011<br />25<br />
  26. 26. Preserve Whole Object<br />Motivation<br />參數為某一物件中取出, 則將該物件當作輸入參數<br />優點:<br />避免新增修改參數項<br />減少參數數量, 方便其他程式呼叫使用<br />條件:<br />因依存關係導致結構惡化則不可使用<br />Example<br />Classification 5/26/2011<br />26<br />
  27. 27. Classification 5/26/2011<br />27<br />
  28. 28. Classification 5/26/2011<br />28<br />
  29. 29. Function Parameters<br />Add Parameters<br />Remove Parameters<br />Separate Query From Modifier<br />Parameterize Method<br />Replace Parameter with Explicit Method<br />Preserve Whole Object<br />Replace Parameter with Method<br />Introduce Parameter Object<br />Classification 5/26/2011<br />29<br />
  30. 30. Replace Parameter with Methods<br />Motivation<br />物件換起某個函式, 並將所得結果作為參數, 傳遞給另一個函式<br />接受該參數的函式也可以喚起前一個函式<br />條件:<br />接收端是否可以透過計算而取得函數值<br />優點:<br />減少參數數量<br />使函式容易理解<br />Example<br />Classification 5/26/2011<br />30<br />
  31. 31. Classification 5/26/2011<br />31<br />
  32. 32. Classification 5/26/2011<br />32<br />Inline Method (117)<br />
  33. 33. Function Parameters<br />Add Parameters<br />Remove Parameters<br />Separate Query From Modifier<br />Parameterize Method<br />Replace Parameter with Explicit Method<br />Preserve Whole Object<br />Replace Parameter with Method<br />Introduce Parameter Object<br />Classification 5/26/2011<br />33<br />
  34. 34. Introduce Parameter Object<br />Motivation<br />一組參數一起被傳遞<br />將一組參數組織再一起<br />[Allen] 解決 Add Parameter 的問題<br />優點:<br />減少參數量<br />找出部分程式碼可移到輸入的類別中<br />Example<br />Classification 5/26/2011<br />34<br />
  35. 35. Classification 5/26/2011<br />35<br />
  36. 36. Classification 5/26/2011<br />36<br />
  37. 37. Classification 5/26/2011<br />37<br />
  38. 38. Classification 5/26/2011<br />38<br />[Allen] 直接存取物件內的資料<br />_start<br />_end<br />
  39. 39. Private Function<br />Remove Setting Method<br />Hide Method<br />Classification 5/26/2011<br />39<br />
  40. 40. Hide Method<br />Motivation<br />類別中某一函式從來沒有被其他類別使用<br />利用IDE工具找出是否有被外部程式Reference<br />若沒有則將其設為Private function<br />
  41. 41. Constructor<br />Remove Setting Method<br />Replace Constructor with Factory Method<br />Encapsulate Downcast<br />Classification 5/26/2011<br />41<br />
  42. 42. Remove Setting Method<br />Motivation<br />物件中某欄位, 應該在初創時被設置, 然後不再改變<br />如果不希望再被改變, 則直接不提供 setXXX<br />[Allen] 專用於Final 的變數<br />Example<br />Classification 5/26/2011<br />42<br />
  43. 43. Constructor<br />Remove Setting Method<br />Replace Constructor with Factory Method<br />Encapsulate Downcast<br />Classification 5/26/2011<br />43<br />
  44. 44. Replace Constructor with Factory Method<br />Motivation<br />當需要使用 Type Code 創建建構式<br />使用Factory Method實做建構式<br />[Allen] 依照不同Type有不同行為時<br />Example * 3<br />Classification 5/26/2011<br />44<br />
  45. 45. Classification 5/26/2011<br />45<br /><ul><li>Example1: 根據整數建構</li></li></ul><li>Classification 5/26/2011<br />46<br /><ul><li>Example2: 根據字串建構</li></li></ul><li>Classification 5/26/2011<br />47<br /><ul><li>Example3: 根據明確函式建構</li></li></ul><li>Constructor<br />Remove Setting Method<br />Replace Constructor with Factory Method<br />Encapsulate Downcast<br />Classification 5/26/2011<br />48<br />
  46. 46. Encapsulate Downcast<br />Motivation<br />優點:<br />將轉型動作封裝<br />[Allen] 避免外部程式自行轉型<br />Example<br />Classification 5/26/2011<br />49<br />
  47. 47. Classification 5/26/2011<br />50<br />
  48. 48. Exception<br />Replace Error Code with Exception<br />Replace Exception with Test<br />Classification 5/26/2011<br />51<br />
  49. 49. Replace Error Code with Exception<br />Motivation<br />[Allen] 與Replace Error Code with Exception相反<br />優點:<br />將異常與一般程式分開<br />Example<br />Classification 5/26/2011<br />52<br />平行<br />
  50. 50. Classification 5/26/2011<br />53<br />說明清楚<br />
  51. 51. Overview<br />Exception<br />Replace Error Code with Exception<br />Replace Exception with Test<br />Classification 5/26/2011<br />54<br />
  52. 52. Replace Exception with Test<br />Motivation<br />[Allen] 與Replace Error Code with Exception相反<br />避免Exception濫用<br />呼叫函式前先檢查必要條件<br />Example<br />Classification 5/26/2011<br />55<br />
  53. 53. Classification 5/26/2011<br />56<br />
  54. 54. Classification 5/26/2011<br />57<br />
  55. 55. Classification 5/26/2011<br />58<br />
  56. 56. Summary<br />Classification 5/26/2011<br />59<br />
  57. 57. Thank YOU !<br />Classification 5/26/2011<br />60<br />

×