DEV Community

ruthmoog
ruthmoog

Posted on • Edited on

Book Club: "Test Driven Development: By Example" #15 - TDD: Letting the compiler direct you

Book Club: "Test Driven Development: By Example" #15

Chapter 15: Mixed Currencies

One thing that makes my life easier when writing code test-first (and there are many things), is when I'm able to write the test I want and let the compiler work for me.

In this chapter we wrote a test for multi-currency conversion, and it allowed us to change a type in our wishful test, then follow the compiler prompt-by-prompt to complete the change.

We wanted to change the type of one of our objects, and call a plus method to add two amounts together, so writing the code below nudges me to create the plus method on Expression:

Expression fiveBucks = new Money.dollar(5);
...
Money result = bank.reduce(fiveBucks.plus(tenFrancs), "USD");
Enter fullscreen mode Exit fullscreen mode

That's great and then I can add the implementation in Expressions sub-classes, no sweat.

🔎 View my code-along repo at https://github.com/ruthmoog/test-driven-development-by-example


Book cover Kent Beck's "Test Driven Development: By Example" was released in 2002. The book aims to explain how to use TDD to write quality code that works.

Top comments (0)