This document discusses strategies for working with legacy code, including:
1. Adding tests by first venting frustrations, then verifying code around new code and adding code seam by seam.
2. Finding seams like objects, linkers, and preprocessors to change behavior without editing code.
3. Adapting parameters by creating new interfaces that can be faked to avoid over-mocking.
4. Extracting and overriding specific calls to allow them to be replaced in tests.
5. Using static setters to allow subclassing singletons and resetting for clean tests.
6. Writing higher level characterization tests as a last resort when refactoring proves difficult.
9. e void paintBackgroundDisabledAndWindowMaximized(Graphics2D g) {
roundRect = decodeRoundRect1();
g.setPaint(decodeGradient1(roundRect));
g.fill(roundRect);
roundRect = decodeRoundRect2();
g.setPaint(decodeGradient2(roundRect));
g.fill(roundRect);
rect = decodeRect1();
g.setPaint(color4);
g.fill(rect);
rect = decodeRect2();
g.setPaint(color5);
g.fill(rect);
rect = decodeRect3();
Saturday, February 15, 14
10. Adapt Parameter
Create New Interface That you can fake
Wrap Hard to instantiate class
Move to interfaces that communicate
responsibilities not implementation - to
avoid over-mocking
Saturday, February 15, 14
11. e void paintBackgroundDisabledAndWindowMaximized(GraphicsAdapter g) {
roundRect = decodeRoundRect1();
g.setPaint(decodeGradient1(roundRect));
g.fill(roundRect);
roundRect = decodeRoundRect2();
g.setPaint(decodeGradient2(roundRect));
g.fill(roundRect);
rect = decodeRect1();
g.setPaint(color4);
g.fill(rect);
rect = decodeRect2();
g.setPaint(color5);
g.fill(rect);
rect = decodeRect3();
Saturday, February 15, 14
12. public class StubGraphicsAdapter implements GraphicsAdapter {
public void setPaint(java.awt.Color color) {
}
...
}
public class JavaAwtGraphicsAdapter implements GraphicsAdapter {
private Graphics2D g;
public JavaAwtGraphicsAdapter(Graphics2D g) {
this.g = g;
}
public void setPaint(java.awt.Color color) {
this.g.setPaint(color);
}
Saturday, February 15, 14
22. Resources
Working Effectively With Legacy Code
- Michael Feathers
http://www.reddit.com/r/badcode
@paytonrules (that’s me)
Bullets suck
Saturday, February 15, 14