Using Java Code Katas to practise Test Driven Development
There is a theory that it takes 10,000 hours to truly master a skill or to make you an expert in something. In the world of programming, by the time you have spent 10,000 hours on something, what you were hoping to be an expert in will probably be deprecated. But the underlying point remains – the best way to improve with a tool, product or methodology, is to practise. Unfortunately, it isn’t always possible to devote time to bettering your skills at the expense of paid work – so how can you get the most benefit in the shortest amount of time?
Here at Manifesto, we are fortunate enough to have the opportunity to get together as a Java team for a couple of hours on a Friday to share knowledge and learn new things as a team.
One of the early sessions we did as a group was on Test Driven Development, something everyone had experience with to varying degrees. It was clear that it was something we wanted to spent time improving going forward, but that mastering it would take time. We decided that as a group we would like to spend some of our Friday sessions practicing our TDD skills, and we decided that using Katas would be the perfect way to do so.
What are Katas?
The term Code Kata is attributed to Dave Thomas, co-author of the book The Pragmatic Programmer. The word kata comes from karate, where students repeat forms over and over and over again, adjusting and improving as they do so.
Code Katas are small, simple coding and thought exercises that can be repeated over time to drive improvements in technique and design. The best Katas are reasonably open, with multiple paths to a solution, or a variety of solutions. With a Kata it’s the journey that is valued, not the destination.
Katas and Test Driven Development
Katas are ideal for practising pure Test Driven Development. They are small problems, often starting in a simple place with added restrictions or complications added in later. This mirrors a TDD testing approach: start with a simple test, make it work, try a more complex test, make it work, and so on, refactoring as you go, until you reach a final solution. There are no external libraries or frameworks to integrate with, so you can focus on a pure Java problem-solving task.
Katas in a Team
Generally, Katas are completed as a solo development task where you repeat over an exercise to improve your methodology along the way. For our sessions however, we have been working as a team, with one member driving the session via a big screen.
Whilst at times it can be frustrating being back-seat developed, it allows for discussion around best practise as you go, with different points of view giving insight into how to tackle or approach a problem. It forces you to consider your habits and helps you see where you can be saving time or being more efficient, and ultimately, become a better developer.
Team Manifesto’s Kata Exercises
As a Java team we have tackled three Katas so far.
The Berlin Clock Kata
If you ever wanted to know the most complex way to display the current time, look no further! The Berlin Clock is a clock face that consists of a number of rows of lights, representing minutes, blocks of five minutes, hours and so on. This Kata asks you to model such a clock.
Bowling Game Kata
Whether you are scoring strike after strike or simply rolling gutter balls, bowling is a game with a deceptively complex scoring method, with frames counting the result of two balls, accounting for spares or strikes feeding back into the previous rounds, and the possibility of three attempts in the last frame for that perfect game. The Bowling Game Kata asks you to design a method for calculating the score at the end of a game. We won’t judge you if you want to keep the sides up!
Supermarket Pricing Kata
Everyone loves a deal, from Buy One Get One Free to Three for a Pound, but how do you build a system that can cope with calculating the price of a basket of goods, and take into consideration all of these promotions? That’s the problem the Supermarket Pricing Kata asks you to solve – if you find any mis-pricings, keep them to yourself!
Give it a shot!
Everyone here in Team Java at Manifesto has enjoyed spending some time using these Katas to brush up their skills, with a particular focus on Test Driven Development. Give it a go, and let us know if you have any favourite Code Katas we have missed in the comments below.