Chapter 2 . The Refactoring Cycle


Chapter 2. The Refactoring Cycle

Exercise 1. Small Steps

Most refactorings reflect this attitude (safety even in mid-refactoring). Consider our Encapsulate Field example. Even this simple program compiled three times. Or look at Extract Method : rather than simply cutting the old code and moving it, Fowler's catalog suggests copying the code and adjusting it before deleting the old version.

Exercise 2. Simple Design

A.

  1. Passes all tests. "If it doesn't have to work, I can give it to you right now."

  2. Communicates. This makes an appeal to our intuition about future readers of our code (including ourselves ).

  3. No duplication. Duplicate code is asking for trouble; it's too vulnerable to changes in one place but not in the other.

  4. Fewest classes and methods . All things being equal, we prefer smaller code.

B. For me, the bottom line is that there's an appeal to the code reader's ability to understand; we'll tolerate duplication to achieve better understanding.

Robert Wenner (personal communication) notes that Java Native Interface (JNI) code, linking Java to C, will have duplication between methods, but each method is just different enough to make it difficult to pursue . Each method has to deal with different arguments and different return values for each function it accesses .

Test code will sometimes have duplication for communication reasons. For example, it may be easier to repeat an expected value rather than to assign it to a variable and use the variable. That way, when you read the code, you know exactly what it was looking for, and you don't have to review code to find the variable and make sure nothing else changed it along the way.




Refactoring Workbook
Refactoring Workbook
ISBN: 0321109295
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net