Last week my colleagues at Gutefrage.net and I had our second Coding Dojo.
I learned a important lessons - even though this wasn’t the first time I practiced the kata.
The task was to create a class that decomposes natural numbers into its prime factors.
Additionally the factors should be sorted ascending.
You get the idea ;)
This sounds simple and like a perfect candidate for an introduction to TDD right?
Unfortunately this kata is a bit tricky.
The first tests are straightforward:
Now decomposing 5 would be a nice test? Right? Since 5 is a prime it would only return 5. But how can we find out if its a prime?
So we could write a function that finds out if 5 is prime. Hm that seems hard.
What about 6? But then we have to develop a algorithm that works - otherwise we would just move sidewards and add conditionals.
You do not know how to solve the problem. Not everything is as simple as the FizzBuzz Kata.
Adding another test alone does not suffice. It’s a common hurdle for newcomers to TDD.
Grab a sheet of paper and think about the algorithm. Or do a little spike until you fully understand the problem. Sometimes it’s just not true, that a design “magically” emerges.
You need a sound knowledge about where to go - otherwise you will have problems with TDD along the way. Then you have to lean back and think a little bit or get feedback from something else then your tests.
TDD is cool, but it can not solve every problem. Especially not complicated algorithms (this is a really easy one of course).
Another personal learning for me was to watch myself better, when I do a kata the first time.
When the design does not emerge from the tests - I should listen to them. Maybe they want to tell me that I have absolutely no idea what I am doing and that I need to think more..