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

Post on 15-Dec-2014

920 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 1

Refactoring

10 – Making Method Calls Simpler

Allen Chien

2011-05-15

Copyright 2009 Trend Micro Inc.

Agenda

• Function Name

• Function Parameter

• Private Function

• Constructor

• Exception

• private example(int param)

Classification 04/10/2023 2

Copyright 2009 Trend Micro Inc.

Function Name

• Rename Method

Classification 04/10/2023 3

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

Copyright 2009 Trend Micro Inc.

Private Function

• Hide Method

Classification 04/10/2023 5

Copyright 2009 Trend Micro Inc.

Constructor

• Remove Setting Method

• Replace Constructor with Factory Method

• Encapsulate Downcast

Classification 04/10/2023 6

Copyright 2009 Trend Micro Inc.

Exception

• Replace Error Code with Exception

• Replace Exception with Test

Classification 04/10/2023 7

Copyright 2009 Trend Micro Inc.

Function Name

• Rename Method– ( 忽略 “做法” )

Classification 04/10/2023 8

Copyright 2009 Trend Micro Inc.

Rename Method

• Motivation– 函式名稱應該準確表達它的用途– 為函式寫上一句註釋 , 然後為該註釋給予一個名稱

• Example

Classification 04/10/2023 9

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

Copyright 2009 Trend Micro Inc.

Add Parameter

• Motivation– 修改函式後 , 需要增加資訊

– 壞味道 : Data Clumps (P81)– 建議 : Introduce Parameter Object (295)

Classification 04/10/2023 11

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

Copyright 2009 Trend Micro Inc.

Remove Parameter

• Motivation– 修改函式後 , 去除參數的重構– 在多型的情況下 , 需要檢查該函式是否已被其他程式實做

Classification 04/10/2023 13

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

Copyright 2009 Trend Micro Inc.

Separate Query from Modifier

• Motivation– 某個函式既回傳物件狀態值 , 又修改物件狀態 (getXXX +

setXXX)– 既有返回值又有副作用就應該分離 (getXXX only)

– Meyer’s Rule: 任何有返回值的函式皆不應有副作用

– 優點 :• 增加重複查詢的效能• 總是獲得相同的結果• [Allen] 函式名稱與期望結果一致

• Example

Classification 04/10/2023 15

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 16

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 17

Substitute Algorithm (139)

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

Copyright 2009 Trend Micro Inc.

Parameterize Method

• Motivation– 問題 : 某個函式做類似的工作 , 但是函式本體卻包含不同的值– 方式

• 使用單一函式 , 並以參數來表達不同的值• 將少量數值視為參數 , 找出重覆的程式碼

– 優點 : 減少重複的程式碼– [Allen] 與 Replace Parameter with Explicit Method 相反

• Example

Classification 04/10/2023 19

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 20

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 21

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

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

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 24

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

Copyright 2009 Trend Micro Inc.

Preserve Whole Object

• Motivation– 參數為某一物件中取出 , 則將該物件當作輸入參數

– 優點 :• 避免新增修改參數項• 減少參數數量 , 方便其他程式呼叫使用

– 條件 :• 因依存關係導致結構惡化則不可使用

• Example

Classification 04/10/2023 26

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 27

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 28

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

Copyright 2009 Trend Micro Inc.

Replace Parameter with Methods

• Motivation– 物件換起某個函式 , 並將所得結果作為參數 , 傳遞給另一個函式– 接受該參數的函式也可以喚起前一個函式– 條件 :

• 接收端是否可以透過計算而取得函數值

– 優點 :• 減少參數數量• 使函式容易理解

• Example

Classification 04/10/2023 30

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 31

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 32

Inline Method (117)

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

Copyright 2009 Trend Micro Inc.

Introduce Parameter Object

• Motivation– 一組參數一起被傳遞– 將一組參數組織再一起– [Allen] 解決 Add Parameter 的問題– 優點 :

• 減少參數量• 找出部分程式碼可移到輸入的類別中

• Example

Classification 04/10/2023 34

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 35

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 36

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 37

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 38

[Allen] 直接存取物件內的資料_start_end

Copyright 2009 Trend Micro Inc.

Private Function

• Remove Setting Method

• Hide Method

Classification 04/10/2023 39

Copyright 2009 Trend Micro Inc.

Hide Method

• Motivation– 類別中某一函式從來沒有被其他類別使用– 利用 IDE 工具找出是否有被外部程式 Reference– 若沒有則將其設為 Private function

Copyright 2009 Trend Micro Inc.

Constructor

• Remove Setting Method

• Replace Constructor with Factory Method

• Encapsulate Downcast

Classification 04/10/2023 41

Copyright 2009 Trend Micro Inc.

Remove Setting Method

• Motivation– 物件中某欄位 , 應該在初創時被設置 , 然後不再改變– 如果不希望再被改變 , 則直接不提供 setXXX– [Allen] 專用於 Final 的變數

• Example

Classification 04/10/2023 42

Copyright 2009 Trend Micro Inc.

Constructor

• Remove Setting Method

• Replace Constructor with Factory Method

• Encapsulate Downcast

Classification 04/10/2023 43

Copyright 2009 Trend Micro Inc.

Replace Constructor with Factory Method

• Motivation– 當需要使用 Type Code 創建建構式– 使用 Factory Method 實做建構式– [Allen] 依照不同 Type 有不同行為時

• Example * 3

Classification 04/10/2023 44

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 45

• Example1: 根據整數建構

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 46

• Example2: 根據字串建構

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 47

• Example3: 根據明確函式建構

Copyright 2009 Trend Micro Inc.

Constructor

• Remove Setting Method

• Replace Constructor with Factory Method

• Encapsulate Downcast

Classification 04/10/2023 48

Copyright 2009 Trend Micro Inc.

Encapsulate Downcast

• Motivation– 優點 :

• 將轉型動作封裝• [Allen] 避免外部程式自行轉型

• Example

Classification 04/10/2023 49

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 50

Copyright 2009 Trend Micro Inc.

Exception

• Replace Error Code with Exception

• Replace Exception with Test

Classification 04/10/2023 51

Copyright 2009 Trend Micro Inc.

Replace Error Code with Exception

• Motivation– [Allen] 與 Replace Error Code with Exception 相反– 優點 :

• 將異常與一般程式分開

• Example

Classification 04/10/2023 52

平行

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 53

說明清楚

Copyright 2009 Trend Micro Inc.

Overview

• Exception– Replace Error Code with Exception– Replace Exception with Test

Classification 04/10/2023 54

Copyright 2009 Trend Micro Inc.

Replace Exception with Test

• Motivation– [Allen] 與 Replace Error Code with Exception 相反– 避免 Exception 濫用– 呼叫函式前先檢查必要條件

• Example

Classification 04/10/2023 55

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 56

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 57

Copyright 2009 Trend Micro Inc.Classification 04/10/2023 58

Copyright 2009 Trend Micro Inc.

Summary

Classification 04/10/2023 59

Copyright 2009 Trend Micro Inc.

THANK YOU !

Classification 04/10/2023 60

top related