Tradeoffs


Earlier, I mentioned that it is a good thing to be able to see the mock behavior right in the test itself. Then I had you extract the mock definition to a separate method in order to eliminate the duplication that was impending with the addition of another test method that needed a similar mock. This was at a cost of some expressiveness, although it's reasonably clear what the code

 account.setAch(createMockAch(AchStatus.SUCCESS)); 

is doing.

Which is worse? Duplication or code that is hard to follow? The answer isn't always clear cut. The question often engenders debate. From my perspective, duplicate code is usually more troublesome than difficult-to-read code. You don't often need to choose one over the other, but when you do, eliminating duplication is a safer route. By eliminating duplicate code, you reduce the cost of maintenance effortsyou avoid having to make changes in more than one place. You also minimize the risk of making a change in one place but forgetting to make it in another place.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

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