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

14
Copyright 2011 Trend Micro Inc. Internal Use Only 06/07/2022 1 Ch 6: Composing Methods Refactoring: Improving The Design of Existing Code

description

 

Transcript of 重構—改善既有程式的設計(chapter 6)

Page 1: 重構—改善既有程式的設計(chapter 6)

Copyright 2011 Trend Micro Inc.Internal Use Only 04/10/2023 1

Ch 6: Composing Methods

Refactoring: Improving The Design of Existing Code

Page 2: 重構—改善既有程式的設計(chapter 6)

Copyright 2011 Trend Micro Inc.Internal Use Only 04/10/2023 2

Ch6: Composing Methods

6.1 Extract Method

6.2 Inline Method

6.3 Inline Temp

6.4 Replace Temp with Query

6.5 Introduce Explaining Variable

6.6 Split Temporary Variable

6.7 Remove Assignments to Parameters

6.8 Replace Method with Method Object

6.9 Substitute Algorithm

Page 3: 重構—改善既有程式的設計(chapter 6)

Copyright 2011 Trend Micro Inc.

6.1 Extract Method

• 將一段程式碼放進一個獨立函式中,並讓函式名稱解釋該函式的用途。

• Motivation– Reusability– Readability– Easier to override

Internal Use Only 04/10/2023 3

Page 4: 重構—改善既有程式的設計(chapter 6)

Copyright 2011 Trend Micro Inc.

6.1 Extract Method

• Mechanics– Create a new method, name it according to its purpose– Copy code to new method– Check local variables– Check temporary variables– Local variables modified by target code block?– Pass in local variables needed by new method as

parameters– Compile new method– Replace code block with new method– Compile and test

Internal Use Only 04/10/2023 4

Page 5: 重構—改善既有程式的設計(chapter 6)

Copyright 2011 Trend Micro Inc.

6.2 Inline Method

• 在函式呼叫點插入函式本體,然後移除該函式。• Motivation

– Remove redundant redirection

• Mechanics– Make sure the method is not polymorphic– Find all of its callers– Replace all calls to it with its code– Compile and test– Delete the method definition

Internal Use Only 04/10/2023 5

Page 6: 重構—改善既有程式的設計(chapter 6)

Copyright 2011 Trend Micro Inc.

6.3 Inline Temp

• 將所有對只被附值一次暫時變數的引用動作,替換為對它賦值的那個運算式本身。

• Motivation– Remove temporary variables that block refactoring

• Mechanics– If the temporary variable is not final, declare it as final and

compile– Replace all references to it with its initialization– Compile and test after each replacement– Delete the variable declaration and initialization– Compile and test

Internal Use Only 04/10/2023 6

Page 7: 重構—改善既有程式的設計(chapter 6)

Copyright 2011 Trend Micro Inc.

6.4 Replace Temp with Query

• 將對暫時變數附值的運算式提煉到一個獨立函式中。將這個暫時變數的所有「被引用點」替換為「對新函式的呼叫」。新函式可被其他函式使用。

• Motivation– Make the query available to the class

• Mechanics– Find a temporary variable that has been assign once only– Declare it as final and compile– Move the initialization code to a new method– Compile and test– Do Inline Temp on the temporary variable

Internal Use Only 04/10/2023 7

Page 8: 重構—改善既有程式的設計(chapter 6)

Copyright 2011 Trend Micro Inc.

6.5 Introduce Explaining Variable

• 將該複雜運算式〈或其中一部分〉的結果放進一個暫時變數,以此變數名稱來解釋運算式用途。

• Motivation– Use a temporary variable to help explain the purpose of an

operation

• Mechanics– Declare a final temporary variable to store the result of an

operation– Replace the operation with a reference to the temporary

variable– Compile and test– Repeat on other operations

Internal Use Only 04/10/2023 8

Page 9: 重構—改善既有程式的設計(chapter 6)

Copyright 2011 Trend Micro Inc.

6.6 Split Temporary Variable

• 針對每次賦值,創造一個獨立的、對應的暫時變數。• Motivation

– Avoid using a temporary variable for multiple purposes

• Mechanics– Rename the temporary variable at its declaration and

initialization– Declare new temporary variable as final– Replace references to the temporary variable before its

second assignment with the new one– Re-declare the temporary variable at its second assignment– Compile and test– Repeat on other temporary variables

Internal Use Only 04/10/2023 9

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

Copyright 2011 Trend Micro Inc.

6.7 Remove Assignments to Parameters

• 以一個暫時變數取代一個參數的位置。• Motivation

– Avoid assigning new values to parameters (passed by value)

• Mechanics– Create a temporary variable to store the new value for the

parameter– Replace all references to the parameter after the

assignment with reference to the temporary variable– Edit the assignment statement to assign to the temporary

variable– Compile and test

Internal Use Only 04/10/2023 10

Page 11: 重構—改善既有程式的設計(chapter 6)

Copyright 2011 Trend Micro Inc.

6.8 Replace Method with Method Object

• 將大型函式放進一個獨立物件中,如此一來區域變數就成了物件內的欄位。然後你可以在同一個物件中將這個大型函式分解為數個小型函式。

• Motivation– Overcome the difficulty of handling local variables during

method breakdown

Internal Use Only 04/10/2023 11

Page 12: 重構—改善既有程式的設計(chapter 6)

Copyright 2011 Trend Micro Inc.

6.8 Replace Method with Method Object

• Mechanics– Create a new class, name it according to its purpose– Create a final field for the source object, and create a field

for each parameter and each temporary variable– Add a constructor to receive source object reference and all

parameters– Add a compute() method– Copy code to compute(), use the reference to the source

object when calling any of its methods.– Compile– Replace the original method with a comment:

• Create an object instance of the new class, and then call its compute() method.

Internal Use Only 04/10/2023 12

Page 13: 重構—改善既有程式的設計(chapter 6)

Copyright 2011 Trend Micro Inc.

6.9 Substitute Algorithm

• 將函式本體替換為另一個演算法。• Motivation

– Clarity

• Mechanics– Compile replacement code– Run unit tests– If test results are different, use old algorithm as basis for

debugging

Internal Use Only 04/10/2023 13

Page 14: 重構—改善既有程式的設計(chapter 6)

Copyright 2011 Trend Micro Inc.Internal Use Only 04/10/2023 14