Why Katas Are More Than Finger Exercises
A kata is an architecture lab in miniature – because the task stays constant, it reveals what otherwise only the design knows.
Why Katas Are More Than Finger Exercises
In martial arts, a kata is a fixed sequence of movements that you repeat until it runs without thinking. Dave Thomas brought the term into programming a good ten years ago: small, sharply defined tasks that you don’t solve once but again and again – FizzBuzz, the bowling score, Roy Osherove’s String Calculator. codekata.com has a whole collection of them.
The comparison with martial arts hits one point very precisely and clearly misses another. Where it hits: a kata takes place without anything at stake. No customer is waiting, no deadline is pressing, no legacy system is interfering. You can pick a single thing and give it your full attention. Where it falls short: in martial arts, the goal of repetition is for the movement to become ever more identical. In programming, that would be pointless. Typing isn’t the bottleneck, and if you write down the bowling score identically for the fifth time, you have learned nothing the fifth time.
The repetition pays off for a different reason. Because the task stays constant, the design can vary – and that is exactly how design decisions become visible that stay invisible in everyday project work. A kata isn’t typing practice. It is an architecture lab in miniature.
A lab needs a constant#
If you seriously want to compare two designs, you have to hold everything else fixed. In a project, that is practically never possible. There, the problem always changes between two approaches too: new requirements, different colleagues, shifted priorities. If the second solution looks better than the first, nobody knows whether the design was better or just the circumstances more favorable. That is why you can rarely read cleanly from a project what a single design decision really cost or brought.
The kata turns this around. The problem is small, completely known, and stays exactly the same. The requirements are fixed before the first line is written. What varies between two runs is exclusively the design: where does the state live? What is a function, what is an object? Which boundary separates logic from input and output? If run three is easier to test than run one, it isn’t because of the task. It is because of a decision I made – and that I can now name.
That sounds trivial, but it isn’t. Only through repetitions like these did I notice how many of my design habits were simply habits: things I did because I always did them that way, not because they served the task. In a project, something like that goes unnoticed, because nobody ever solves the same task a second time in a different way. In a kata, it is noticed immediately.
Two shelves full of attempts#
My GitHub account holds two such collections from recent years. The older repository is simply called Katas, dates from 2010, and contains C# exercises: a tennis kata and a FizzBuzz variant that I built with the Reactive Extensions back then – not because FizzBuzz needs that, but because I wanted to know how the same trivial task feels when you think of it as a stream of events instead of a loop. There is no README; the repository is a personal practice archive and doesn’t want to be anything else.
The younger one is called AppKatas, dates from 2013, and is written in JavaScript. Among other things, it contains a calculator kata and a TicTacToe kata, both deliberately implemented as pure JavaScript with Event Sourcing; the modules run in the browser and under Node.js, and the tests run on Travis. TicTacToe exists there in two stages: as a pure browser application and as a client-server variant with a small Node server.
The value of these repositories doesn’t lie in the code. Today, I would write it differently in many places. The value lies in the fact that the same tasks, solved several times and in different ways, sit side by side there. You can compare the designs because the task stayed constant. That is exactly what I mean by a lab.
Testability is design pressure#
The tennis kata looks harmless: count the points of a tennis game and output the score – Love, Fifteen, Thirty, Forty, Deuce, Advantage. In the first run in 2010, I built it as a class with two counters and one big method that assembles the score as text. The first NUnit test for it looked roughly like this:
[TestFixture]
public class TennisGameTests
{
[Test]
public void Score_AfterDeuce_ReturnsAdvantage()
{
var game = new TennisGame();
for (var i = 0; i < 3; i++)
{
game.WinPoint(Player.One);
game.WinPoint(Player.Two);
}
game.WinPoint(Player.One);
Assert.AreEqual("Advantage Player One", game.Score());
}
}
The test works, but it gives something away: to check a single score, I have to play seven points in the right order. The state is locked inside the object, and the only way in goes through the history. With deuce, that is still bearable. With a task that has more state, exactly this pattern becomes torture – every test begins with a long setup that has nothing to do with the actual assertion.
That is the moment when testability becomes design pressure. The test that is hard to write isn’t a testing problem; it is a symptom. It shows that the design doesn’t allow direct access to the state that matters for the domain. In the second run, I modeled the score as a value you can construct directly, and the transition logic as a function from state to state. The tests shrank to three lines, and along the way, the big text method had fallen apart into small, nameable cases.
In a project, I would never have done this second run. After all, the first version worked. Nobody rebuilds a working piece of software just to see whether the design could also be different. In a kata, that is exactly the purpose of the exercise.
The same task, two designs#
I saw the lab effect most clearly with TicTacToe in AppKatas. The obvious first version in JavaScript looks the way most people would probably write it – a game object that holds a board and changes it with every move:
function Game() {
this.board = [null, null, null, null, null, null, null, null, null];
this.currentPlayer = "X";
}
Game.prototype.move = function (field) {
if (this.board[field] !== null) {
throw new Error("field already taken");
}
this.board[field] = this.currentPlayer;
this.currentPlayer = this.currentPlayer === "X" ? "O" : "X";
};
That is perfectly fine and easily carries a small game. It gets interesting in the second run with a different fundamental decision: the game doesn’t store a board, only the list of moves. The board is derived from the events when needed:
function replay(events) {
var board = [null, null, null, null, null, null, null, null, null];
events.forEach(function (event) {
board[event.field] = event.player;
});
return board;
}
function move(events, field) {
var board = replay(events);
if (board[field] !== null) {
throw new Error("field already taken");
}
var player = events.length % 2 === 0 ? "X" : "O";
return events.concat([{ player: player, field: field }]);
}
No this, no mutation, no constructor. move takes a list and returns a new list. The consequences of this one decision practically fall into your lap: a test is now a list in, a list out – you can write down any game state directly without having to play moves. Undo means leaving out the last element. And the client-server variant became almost trivial, because a list of moves can be sent as JSON, whereas an object with methods and internal state falls apart at the process boundary.
flowchart TD A[Same task:<br/>TicTacToe] --> B[Design 1:<br/>state is mutated] A --> C[Design 2:<br/>list of moves, board derived] B --> D[Test needs<br/>move history as setup] C --> E[Test is<br/>list in, list out] D --> F[Difference is attributable<br/>to the design, because the<br/>problem stayed constant] E --> F
The point isn’t that event lists are better than mutable state. For a TicTacToe in the browser, the first version is simply appropriate, and running replay completely on every move clearly doesn’t scale to large numbers of events. The point is that I can hold both designs side by side against the same task. Only that lets me see what the decision costs and what it buys. In a project, I only ever get to see one of the two branches – the one we chose. The other remains speculation.
Along the way, the second version forces a clean boundary that tends to blur in the first: the game logic knows nothing about the DOM. replay and move run unchanged under Node and in the browser; the rendering is a thin edge that receives events and draws a board. I didn’t draw this boundary because a principle demands it, but because the design pushed it on me. Boundaries that get pushed on you like this stick better than preached ones.
The luxury of throwing away#
A kata includes a step that is unthinkable in a project: at the end, the code is deleted. Not archived, not kept as the basis for the next run – deleted. Next time, you start from scratch.
At first, that struck me as wasteful. By now, I consider it the most important part. As long as code lives on, you defend it. You build on it, work around its weaknesses, get used to its shape. Only when it is certain that it will disappear do you become honest with it. Then you try out the decision you would never risk in a project, because there every decision immediately gets grandfathered in.
That is exactly the difference between practicing and working. In a project, code is a capital good, and every alternative competes with what is already there. In a kata, code is experimental material. You may think an idea through to the end, find that it doesn’t hold, and bury it at no cost. The insight survives; the code doesn’t. That is the right order.
What a round looks like for me#
Over time, a simple format has settled in for me, without any supporting program – one hour, one editor, one task:
- Choose a familiar task, not a new one. Your curiosity should go to the design, not the problem.
- Before the first test, fix a single design decision that turns out differently this time than last time.
- Write the tests first, and read every sluggish test as a hint about the design, not as a testing problem.
- At the end, jot down two or three sentences: what was easier, what was harder, and why?
- Delete the code.
The notes are inconspicuous, but they are the actual result. After a few rounds with the same task, they add up to a small, self-made set of index cards about design decisions – backed by code you wrote yourself instead of taken on faith from a book.
What sticks#
Finger exercise is the wrong word, because it describes the goal wrongly. What gets trained isn’t the hand; it is judgment. A kata holds the problem fixed so that the design can vary, and thereby reveals what a design decision really does: which tests it makes easy or sluggish, which boundaries it forces, what it costs at a process boundary. These are exactly the questions that echo for years in large systems – except that there, you can never observe them in isolation.
That is why I don’t consider the half hour with FizzBuzz, tennis, or TicTacToe wasted time, even though nothing comes out of it that anyone uses. Something else comes out of it: the experience of having seen the same task with two designs. Once you have that, you no longer argue from preferences in the next architecture discussion but from observations. That is more than a finger exercise could ever deliver.
Further reading#
- https://github.com/MikeBild/AppKatas – my JavaScript katas (calculator, TicTacToe with Event Sourcing)
- https://github.com/MikeBild/Katas – my older C# katas (tennis, FizzBuzz with Rx)
- http://codekata.com/ – Dave Thomas’ kata collection, which coined the term
- https://osherove.com/tdd-kata-1 – Roy Osherove’s String Calculator kata
Revised and translated from German with the help of AI.