Blog of Rob Galanakis (@robgalanakis)

TDD via Tic-Tac-Toe

For the last few years, I’ve done a fair bit of teaching of TDD, usually 1-on-1 during pair programming sessions but also through workshops. I’ve tried out lots of different subject matter for teaching TDD, but my favorite has been Tic-Tac-Toe (or whatever your regional variation of it is). It has these benefits:

  1. It’s a game. People like programming a game. Learning through games is good. It also reinforces the idea of ‘testability.’ A number of times I had students combine the ‘playing’ of the game with the ‘logic’ of the game. Teaching them to split out the algorithm from the input/driver was a useful exercise.
  2. It’s not trivial. It is a problem with some real meat that can be solved in a number of different ways. It’s significant enough that a real design and architecture emerges, unlike TDD for a single function. A a bonus, the diverse answers make it fun to teach because I keep being led to new solutions by students.
  3. The rules are just right. Simple and clear. It has a clear ‘happy path’ and ‘unhappy path’.
  4. It’s bounded. You can know when it’s good enough, but there are also endless exceptions to check if you want. Which is a great way to teach people to know when something is complete enough.
  5. It has no dependencies. I prefer not to introduce people to TDD and mocking at the same time.

In the workshops I ran, we did a ‘mob programming’ of a Tic-Tac-Toe game, and then students paired up to develop an AI. While the AI was fun, it was developing the game that was probably a better exercise. And like I mentioned already, I’ve done lots of introductory TDD pairing sessions using it, recently with someone interviewing here which is when I think the Tic-Tac-Toe game really proved itself a successful subject matter. I highly suggest you give Tic-Tac-Toe a try if you’re looking for a way to demonstrate/teach TDD to someone.

If you’re interested, the code and slides I created for the workshops is here: https://github.com/rgalanakis/tddtraining I may make a blog post in the future with some more detail about how the workshops went, if there’s any interest.

2 thoughts on “TDD via Tic-Tac-Toe

  1. Tomek says:

    Hi Rob, thanks for this idea. I have to say I like it. Tic-Tac-Toe is so much simpler than Convey’s game of life, which is used during code retread. I believe you are right – this is a good exercise for TDD novices.

    Some time ago while looking for ideas for TDDs trainings I had the one about “pager”, see http://kaczanowscy.pl/tomek/2013-04/code-kata-pager – I think you might find it interesting.

    Cheers!

    1. Thanks Tomek, the pager example is good but I worry it’s too simple. Especially in Python and other expressive, high level languages. But it is certainly a good first step. I’ve also taught TDD with the Game of Life and while it wasn’t bad, the main problem was that it is just an algorithm. It means the tests can easily just become ‘acceptance tests,’ losing the point of the TDD process. I also ran into this problem with the Tic Tac Toe AI lesson I used. I find it more illustrative to have a simpler algorithm but also more rules and edge cases.

Leave a Reply