During the 15th Meeting of the Softwerkskammer Stuttgart we approached a Kata where the goal is to create an algorithm to transform the input on the left to the output on the right.
In the retrospective for the exercise we learned the Kata, at the first glance hardly challenging, serves as a great example for learning to take small steps. A small step could be regarded as less than 2 minutes passing between writing the failing test and making it pass.
We recite the Three Laws of TDD as a reminder.
All it takes to make it pass is the following implementation
That’s cheating you say, but we have to take a look at the third law of TDD above. Anything other than returning the hardcoded
'A' would have violated the law since it would have been more than is sufficient to make the test pass.
Continuing we move the implementation towards supporting multiple characters by using toUpperCase
A rash next step would be the to add the following test
As to what is the quickest way to make the above test pass. We pretty much have to create the full algorithm in one go.
Why? Because the step we took was too big. Take a breather to think. We would have to extend the algorithm by the following three features to get the test to pass
These three features could have been revealed by giving the test a proper description. A truthful description would be as follows
Quite a mouthful. Having to resort to words such as
and in a test description makes the error blatantly obvious. If the test has multiple reasons to fail it makes it harder for you to pinpoint the introduction of bugs.
Let’s continue with our broken down tests
Make it pass by extending the algorithm
The fact that we have to handle every character guides us towards the introduction of a loop.
On with the last test
And make it pass
Let’s verify the implementation with a more complex test
It passes without changing the implementation.
If you would get the test to pass by pretty much writing the whole algorithm at once consider breaking the test into smaller pieces. If you can’t think of any way to break the test down try to write a more verbose description of what the test does.