Chapter 6 . Duplication


Chapter 6. Duplication

Exercise 13. Two Libraries

A. One strategy:

  • Define a new logger whose interface is compatible with the JDK 1.4 logger. It could be a simplified layer interface or a class with a compatible interface (that in the future would be a subclass of the JDK 1.4 logger), or it might be a straightforward implementation of the new classes.

  • Make the old loggers call the new logger.

  • Modify Log and its callers to become like the new logger, so you can delete the Log class.

  • Modify Logger to become like the new logger, so you can delete the Logger class too.

There will be a temptation to do this relatively slowly (i.e., for now, use the new logger for new and changed code). Note that this adds to our conceptual burden . You might be able to use automated support to make it easier.

Exercise 14. Properties

A. Use Extract Method to pull out a routine that looks up the property, converts it to an integer, and validates it as positive. (Do this in steps: first, second, and third copies.)

You might determine that it's OK to set monitorTime and departureOffset even if the exception will be thrown. This will reduce the need for temps.

The end result might look like this:

 checkInterval = getProperty("interval"); monitorTime = getProperty("duration"); departureOffset = getProperty("departure"); if ((monitorTime % checkInterval) != 0)   throw new MissingPropertiesException(     "duration % checkInterval"); if ((departureOffset % checkInterval) != 0)   throw new MissingPropertiesException(     "departure % checkInterval"); 

You might then extract a separate method to enforce the % restriction.

Exercise 15. Template Example

A. Duplication

  • The whole thing is two nearly identical copies, one for %CODE% and one for %ALTCODE% . However, one case writes to a string, the other to an output stream.

  • The string literal %CODE% and the numeric literal 6 are aspects of the same thing (magic number). Likewise %ALTCODE% and 9 .

  • The construction of the resulting final string for each part is similar: appending a prefix, body, and suffix.

B. Remove duplication

  • Replace Magic Number with Symbolic Constant (and with functions)

  • Extract Method

  • Look into unifying string and input/output (I/O) so we can make the methods even more similar.

C. new String()

  • What does it do? The new causes a new string with identical contents to be created. The new string equals() the old but is a different object.

  • The intern() method returns a unique instance of the string that will be the same object as all other internalized strings with the same content. (By default, strings are not required to be shared.)

  • Does it apply here? No. Since we never compare internalized strings, the new String part is redundant.

Exercise 16. Duplicate Observed Data

A. The duplication is often not as dramatic as it first appears. Often, the domain object has its own object representation of itself, and the widget ends up holding a string or other display representation.

  • The user interface is usually one of the most volatile parts of a program, while the domain classes tend to be modified less often (during development).

  • Putting the domain information in the widget ties them together. A domain class should be able to change its value independent of whether the value is displayed on the screen. (See the Observer pattern.)

  • Mixing domain and screen classes makes the domain depend on its presentation; this is backwards . It's better to have them separate so the domain classes can be used with an entirely different presentation.

B. The performance can go either way. When they're in one object, the domain class updates its value using widget methods. This is typically slower because it must take into account buffering, screen updating, etc.

On the other hand, the synchronization can become relatively costly. Occasionally you have to find a way to make this notification cheaper. In some situations, splitting the domain class and the widget will let the widget avoid being displayed

Exercise 17. Java Libraries

A. Examples

  • AWT versus Swing : There are two whole widget libraries included.

  • Collections versus Vector/HashTable : There are two collection libraries.

  • Event listeners : There are many closely related variations.

  • Duplicate methods in GUI classes; e.g., show() versus setVisible() .

  • Others.

B. Why?

  • The most common reason seems to be that old chestnut ”historical reasons. Sun is understandably reluctant to change published interfaces that many people depend on. Instead of changing things, they add more, even if it overlaps in intent or code.

    Java has a deprecation flag that can be set and is set for some of these duplications. This warns clients that they're depending on the old way of doing things and that there's a better way.

  • In something as big as Java's libraries, there are many people working on them, and they don't always coordinate well enough to realize that they've duplicated work.

  • Java's libraries are very public (with many books and articles describing them in detail). This means they're subjected to more scrutiny than most efforts. In other words, their duplication may be no worse than others, it's just that they're more visible.

Exercise 18. Points

A. Both are using points that wrap around the maxX and maxY values.

B. Substitute Algorithm to make both classes calculate wrapping the same way. Then Extract Class to pull out a WrappingPoint class.

C. The search for duplication can help you identify these situations. You can create a test that reveals the bug in the bad code. While you fix it, you can drive toward similarity to the good code and then use the refactorings that address duplication to clean up the duplication.

Exercise 19. Expression

A. You could use Extract Method on the return statements, so return v1 + v2 ; becomes return value(v1,v2) ; and similarly for * . This makes the value() routines identical, so you can Pull Up Method to bring them to the Composite node. (The two-argument routine will still have to be in the subclasses.)




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