Step 3: Tackle Duplication in the Class

Here’s the MeterPrinter class. What kind of duplication do you see in there? Grab a pencil and circle anything you suspect you may want to refactor. Bonus points if you can figure out what we should do with it after.

images/programming/hammer1.png

Hopefully after scanning this, you were able to identify a few of the following culprits.

It seems like the Meter Report header and the Construction Co. Ltd. footer are added regardless of which type of meter is present (a clear case of copy and paste). The question now is what to do about it.

If your first instinct was to pull this code into a common method somewhere, your instincts are good. ...

Get The Way of the Web Tester now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.