The Advent Of Tdd A Beginners Guide

Starting with Test-Driven Development

How can I start applying Test-Driven Development?” This question echoes through my training sessions like a recursive function call. While my default response has always been “Start with fixing bugs, “ I’ve discovered an even more engaging approach: Advent of Code.

My default approach: fixing an existing bug

Let’s examine a simple example. Imagine you have a calculator function:

Calculator function code example

When someone tries to divide by zero, our application crashes unceremoniously. This presents a perfect opportunity for TDD – we can write a test expressing our desired behavior:

Test code for divide by zero scenario

My test will of course fail, because my code fails. I can now fix my code and make my test pass.

The Gamified Approach: Advent of Code

As a self-proclaimed geek with an affinity for puzzles, I’ve found Advent of Code to be the perfect TDD training ground. Each puzzle provides:

  • An input sample

  • An expected output

This structure naturally guides you into the TDD mindset – you write your test based on the example before implementing your solution. It’s like having a built-in specification!

Advent of Code example showing input and expected output

I mean, not only it’s a good way to learn TDD but it’s also fun. You’re honing your development skills, solving a puzzle and practicing a very valuable technique.

As you advance through the challenges, you might start to experiment more with TDD and maybe reduce the granularity of some of your tests. For example, you might need to parse a complex text input into a Map in order to solve the problem, and then that could be a test on its own.

Tools That Enhance the Experience

In Elixir (my language of choice), the pipe operator (|>) is particularly powerful for TDD:

Elixir pipe operator code example

That small triangle basically says: take the output of the above function and pass it on to the following function. This elegant syntax allows you to sketch out your solution’s high-level flow before diving into implementation details.

Combine this with tools like mix test.watch, which automatically runs tests when files change, and you’ve got a rapid feedback loop that makes TDD feel natural and intuitive.

Ready to Begin?

Pick your preferred programming language and dive in! You can explore my Advent of Code 2024 repository for inspiration: https://github.com/mariomelo/advent_of_code24

Remember: The goal isn’t perfection but practice. Each puzzle is an opportunity to strengthen your TDD muscles while having fun!

For deeper insights into TDD, check out ’s excellent article on Software Design: Tidy First?

Software Design: Tidy First?Canon TDDWhat follows is NOT how you should do TDD. Take responsibility for the quality of your work however you choose, as long as you actually take responsibility…Read more2 years ago · 277 likes · 41 comments · Kent Beck

Happy coding!

The Advent Of Tdd A Beginners Guide
The Advent Of Tdd A Beginners Guide