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