TDD Money Step-by-Step in C#

While preparing a TDD workshop, I ported Kent Beck’s Money example from the book “Test-Driven Development by Example”

  • to C#
  • using .NET Core

I’ve tried to map each change described in the book to a dedicated git commit with a sensible commit message. It turned out being about 100 commits. The “micro-commits” are for teaching & learning (“step-by-step”). Here’s a screenshot:

img

The solution includes the “trick” of extracting an “Expression” interface to deal with money operations:

Ward Cunningham came up with the ‘trick’ a decade ago,and I haven’t seen it independently duplicated yet, so it must be a tricky trick.”

Kent’s solution shows how TDD can lead to well-crafted code. Complying to OO (Object-Oriented) best practices, such as

  • Encapsulation (aka information hiding),
  • “Tell, don’t ask” (aka Hollywood principle),
  • SRP (Single Responsibility Principle),
  • SLA (Single Level of Abstraction)

just to name a few.

After finishing this exercise, I stumbled upon Mark Seeman’s post https://blog.ploeh.dk/2017/10/16/money-monoid/ which shows that this solution also conforms to Functional Programming (FP) best practices. Money is a Monoid. Which is one of the FP pillars for making things composable.

Feel free to use the code to your liking:

https://github.com/draptik/tdd-money-csharp

comments powered by Disqus