DEV Community

ruthmoog
ruthmoog

Posted on

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

Chapter 3: Equality for All

This chapter introduces the Value Object pattern. Value Objects,

  • return a new object from operations
  • implement equals

This pattern is useful for avoiding aliasing, e.g. if you set a number's value to some integer, and set another numbers value to the other number rather than it's own integer. This reminded me of spreadsheet references, which I think makes for a nice visual representation of aliasing.

Spreadsheet showing the number of cats in cell B1 as 5, and the number of dogs in cell B2 as the formula  raw `=B1` endraw The number of dogs in B2 is whatever value is put in B1. If I change the number of cats to 6, uhoh! I would find myself with 6 dogs as well.

Beck also talks about Triangulation, a TDD approach based on the process in mathematics for determining the location of a point. This is nice and robust way of ensuring your test covers the desired behaviour!

  1. positive example behaviour: up is up.
  2. negative example behaviour: up is not down.
  3. generalise equality: up↑ is the same direction as up↑.

Beck says,

I use it only when I am completely unsure of how to refactor ... Triangulation provides a chance to think about the problem from a slightly different direction. What axes of variability are you trying to support in your design?

Sometimes this approach would be overkill - but it's a helpful thinking pattern to build clarity.

🔎 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.

Discussion (0)