TDD - What is a Code Kata?

Most developers have heard of Test Driven Development (TDD). Whether or not they actually practice it is another story. In this post, I would like to disucuss one way of practicing TDD skills. This excercise is known as a Code Kata.

In order to understand what a Code Kata is, we must first understand what the word “Kata” means. A Kata is an exercise used in martial arts in which detailed, choreographed patterns of movements are practiced. Wikipedia explains the purpose of a Kata very well in the below snippet:

By practicing in a repetitive manner the learner develops the ability to execute those techniques and movements in a natural, reflex-like manner. Systematic practice does not mean permanently rigid. The goal is to internalize the movements and techniques of a kata so they can be executed and adapted under different circumstances, without thought or hesitation. A novice’s actions will look uneven and difficult, while a master’s appear simple and smooth.

Now how does this relate to software development? The purpose remains the same. We take a coding exercise and practice solving it in a certain manner. In this case, we are talking about solving a problem using TDD. We want to solve the problem using the TDD practices of Red-Green-Refactor. The goal of the exercise is not to arrive at the correct answer. The goal is to practice the skills of TDD repeatedly so that they become “natural” and “reflex-like” skills which can be “executed and adapted under different circumstances, without thought or hesitation.”

Katas are meant to be practiced on a regular basis. They are not meant to be a one and done exercise. The more you do them, the more natural the techniques will become. I recommend doing the same Code Kata every day until you can code it with your eyes closed. Maybe that is a bit of an exaggeration. The point is that you want to repeat the same problem until you have solved the problem many times, in different ways.

Every time you do the kata, you may come up with a different implementation of the solution. Your third solution will probably be better than your first solution. But every solution should be arrived at in the same manner. By writing failing tests first, writing code to make them pass, and then refactoring the code. And doing this over and over and over again until you can read a new problem and solve it using TDD without even thinking about it. These exercises, if practiced enough, should instill the skills in you and make applying TDD to other problems much easier.

In posts to come, I will walk through an actual Code Kata and how I might go about solving it. In the meantime, there are a number of YouTube videos of Code Kata walkthroughs. Below are a couple videos from The Code Dojo:

Written on October 14, 2017