Internal Use Only 4/22/2011<br />1<br />Ch 6: Composing Methods<br />Refactoring: Improving The Design of Existing Code<br />
Internal Use Only 4/22/2011<br />2<br />Ch6: Composing Methods<br /><ul><li>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</li></li></ul><li>6.1 Extract Method<br />將一段程式碼放進一個獨立函式中,並讓函式名稱解釋該函式的用途。<br />Motivation<br />Re...
6.1 Extract Method<br />Mechanics<br />Create a new method, name it according to its purpose<br />Copy code to new method<...
6.2 Inline Method<br />在函式呼叫點插入函式本體,然後移除該函式。<br />Motivation<br />Remove redundant redirection<br />Mechanics<br />Make su...
6.3 Inline Temp<br />將所有對只被附值一次暫時變數的引用動作,替換為對它賦值的那個運算式本身。<br />Motivation<br />Remove temporary variables that block refac...
6.4 Replace Temp with Query<br />將對暫時變數附值的運算式提煉到一個獨立函式中。將這個暫時變數的所有「被引用點」替換為「對新函式的呼叫」。新函式可被其他函式使用。<br />Motivation<br />Mak...
Upcoming SlideShare
Loading in...5
×

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

935

Published on

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
935
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
61
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

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

  1. 1. Internal Use Only 4/22/2011<br />1<br />Ch 6: Composing Methods<br />Refactoring: Improving The Design of Existing Code<br />
  2. 2. Internal Use Only 4/22/2011<br />2<br />Ch6: Composing Methods<br /><ul><li>6.1 Extract Method
  3. 3. 6.2 Inline Method
  4. 4. 6.3 Inline Temp
  5. 5. 6.4 Replace Temp with Query
  6. 6. 6.5 Introduce Explaining Variable
  7. 7. 6.6 Split Temporary Variable
  8. 8. 6.7 Remove Assignments to Parameters
  9. 9. 6.8 Replace Method with Method Object
  10. 10. 6.9 Substitute Algorithm</li></li></ul><li>6.1 Extract Method<br />將一段程式碼放進一個獨立函式中,並讓函式名稱解釋該函式的用途。<br />Motivation<br />Reusability<br />Readability<br />Easier to override<br />Internal Use Only 4/22/2011<br />3<br />
  11. 11. 6.1 Extract Method<br />Mechanics<br />Create a new method, name it according to its purpose<br />Copy code to new method<br />Check local variables<br />Check temporary variables<br />Local variables modified by target code block?<br />Pass in local variables needed by new method as parameters<br />Compile new method<br />Replace code block with new method<br />Compile and test<br />Internal Use Only 4/22/2011<br />4<br />
  12. 12. 6.2 Inline Method<br />在函式呼叫點插入函式本體,然後移除該函式。<br />Motivation<br />Remove redundant redirection<br />Mechanics<br />Make sure the method is not polymorphic<br />Find all of its callers<br />Replace all calls to it with its code<br />Compile and test<br />Delete the method definition<br />Internal Use Only 4/22/2011<br />5<br />
  13. 13. 6.3 Inline Temp<br />將所有對只被附值一次暫時變數的引用動作,替換為對它賦值的那個運算式本身。<br />Motivation<br />Remove temporary variables that block refactoring<br />Mechanics<br />If the temporary variable is not final, declare it as final and compile<br />Replace all references to it with its initialization<br />Compile and test after each replacement<br />Delete the variable declaration and initialization<br />Compile and test<br />Internal Use Only 4/22/2011<br />6<br />
  14. 14. 6.4 Replace Temp with Query<br />將對暫時變數附值的運算式提煉到一個獨立函式中。將這個暫時變數的所有「被引用點」替換為「對新函式的呼叫」。新函式可被其他函式使用。<br />Motivation<br />Make the query available to the class<br />Mechanics<br />Find a temporary variable that has been assign once only<br />Declare it as final and compile<br />Move the initialization code to a new method<br />Compile and test<br />Do Inline Temp on the temporary variable<br />Internal Use Only 4/22/2011<br />7<br />
  15. 15. 6.5 Introduce Explaining Variable<br />將該複雜運算式〈或其中一部分〉的結果放進一個暫時變數,以此變數名稱來解釋運算式用途。<br />Motivation<br />Use a temporary variable to help explain the purpose of an operation<br />Mechanics<br />Declare a final temporary variable to store the result of an operation<br />Replace the operation with a reference to the temporary variable<br />Compile and test<br />Repeat on other operations<br />Internal Use Only 4/22/2011<br />8<br />
  16. 16. 6.6 Split Temporary Variable<br />針對每次賦值,創造一個獨立的、對應的暫時變數。<br />Motivation<br />Avoid using a temporary variable for multiple purposes<br />Mechanics<br />Rename the temporary variable at its declaration and initialization<br />Declare new temporary variable as final<br />Replace references to the temporary variable before its second assignment with the new one<br />Re-declare the temporary variable at its second assignment<br />Compile and test<br />Repeat on other temporary variables<br />Internal Use Only 4/22/2011<br />9<br />
  17. 17. 6.7 Remove Assignments to Parameters<br />以一個暫時變數取代一個參數的位置。<br />Motivation<br />Avoid assigning new values to parameters (passed by value)<br />Mechanics<br />Create a temporary variable to store the new value for the parameter<br />Replace all references to the parameter after the assignment with reference to the temporary variable<br />Edit the assignment statement to assign to the temporary variable<br />Compile and test<br />Internal Use Only 4/22/2011<br />10<br />
  18. 18. 6.8 Replace Method with Method Object<br />將大型函式放進一個獨立物件中,如此一來區域變數就成了物件內的欄位。然後你可以在同一個物件中將這個大型函式分解為數個小型函式。<br />Motivation<br />Overcome the difficulty of handling local variables during method breakdown<br />Internal Use Only 4/22/2011<br />11<br />
  19. 19. 6.8 Replace Method with Method Object<br />Mechanics<br />Create a new class, name it according to its purpose<br />Create a final field for the source object, and create a field for each parameter and each temporary variable<br />Add a constructor to receive source object reference and all parameters<br />Add a compute() method<br />Copy code to compute(), use the reference to the source object when calling any of its methods.<br />Compile<br />Replace the original method with a comment:<br />Create an object instance of the new class, and then call its compute() method.<br />Internal Use Only 4/22/2011<br />12<br />
  20. 20. 6.9 Substitute Algorithm<br />將函式本體替換為另一個演算法。<br />Motivation<br />Clarity<br />Mechanics<br />Compile replacement code<br />Run unit tests<br />If test results are different, use old algorithm as basis for debugging<br />Internal Use Only 4/22/2011<br />13<br />
  21. 21. Internal Use Only 4/22/2011<br />14<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×