TDD by Example Chapter: 9 Times We're Livin' In


Beck opens by asking how to get rid of the subclasses. He suggests that introducing the concept of "Currency" will help. He considers creating a whole new Currency object but decides that it's too much design right now. A string to represent the different currencies is enough.

He implements currency by passing in a string of "CHF" or "USD" in the constructors of the Franc and Dollar classes. While fixing the errors that resulted in adding a new parameter to the constructors he notices that the implementation of times in Franc is still calling the Franc constructor instead of using the Money.franc factory method. He decides to fix this oversight before continuing with the implementation of the currency. Cleaning this up means that the only place that mentions the constructor of either Dollar or Franc is in the factory methods in Money.

He then writes a small paragraph about the small steps he has taken to get to this point. He worries that his readers will think that he wants them to always work in such small steps. Instead he wants his readers to be able to work this way. If you are unsure TDD allows you take small steps knowing that your code still works. If you have more confidence, maybe you've done this kind of thing before, you can take bigger steps.

He ends by summarizing what was just done and mentions that in the next chapter we can finally can push the implementation of times into the the Money class which will allow the elimination of the Franc and Dollar subclasses.


This chapter captures a taste of the real thing. It matches my experience writing code. I feel like I'm in a groove. And then I notice something amiss. I rod the groove into a ditch. But if I've been doing TDD and working in small steps I can make changes with confidence. Even if I've been doing TDD but working in large steps the changes are scarier.

It's also a great example of Beck's maxim

for each desired change, make the change easy (warning: this may be hard), then make the easy change