Subscribe to Jack of all Languages        RSS Feed

A journey through Code Katas

Icon 2 Comments
In martial arts, a kata is a sequence of moves that is practised over and over. You can use it as a vehicle to hone your technique or develop strength, agility and stamina. You can use it as a warm up exercise or to cool down and stretch. You can use it to train slow, controlled movements or to generate ballistic energy. If you have more than one person you can try out applications for the moves.

Kata has so much to offer but many students of martial arts despise the boredom of mindlessly repeating the same moves over and over. To a certain extent they are right. If all you are doing is mindlessly doing anything over and over it's a waste of time. The real value is when you engage with the process. Have a purpose behind the performance of the kata. Notice what went well, notice what went badly, use your observations to improve in the next iteration.

So, do you know what I did when I first saw the name "Code Kata"? I rolled my eyes at the silly metaphor, assumed it was some sort of gimmicky nonsense and looked no further.

And how wrong I was. I wish I could find the first one I watched but this one by Corey Haines is similar and probably better:

You have to understand that in my day job, my freelance work and my hobby code, I'm a one man band. All of my programming is done in a vacuum. If it's not in a hermetically sealed flask it's pretty close. Don't get me wrong. I read books and blogs. I compare my code with what I have just read. I interact with some great minds on DIC but when I'm actually writing code, I'm on my own. There is nobody to pair with, nobody to review my code and nobody's code for me to review.

So watching a great programmer write a short routine while talking about their decisions along the way opens a world of insight for me. It must be a different experience from pairing but it's as close as I'm going to get.

Next I watched Bob Martin do his prime numbers kata.

I could hardly believe how simple and trivial his first test and implementation are. He starts with a constant. The constant turns into an if. The if turns into a couple more ifs and it's looking a bit like a dogs breakfast. But then an if turns into a while. Then another if changes into a while and the whole lot collapses into a simple nested loop. Great stuff, and all driven by tests that are half a step ahead of the "production" code.

Next up was not so much a kata but a live demo of using InteliJ's refactoring tools (Uncla Bob again):

I already do refactoring, including all of the stuff he showed there. But I never really looked much at my IDE's refactoring tools. I decided a long time ago that keyboard shortcuts aren't worth learning (bad decision). My reasoning was that it's a lot of work to learn the, and they'll be useless when I switch edotors anyway.

I did my refactorings manually or occasionally through the menus. Uncle Bob's video showed just how quick and easy it was to use the built in tools. I use Eclipse and it turns out that all the refactoring shortcuts are very sensibly and memorably arranged. I've been playing with them ever since and they are a pleasure to use.

I have really got a lot from watching these videos and several others. But just like the martial arts katas, I suspect the biggest benefit will be from performing and perfecting them myself. I begun he habit of starting each day with a code kata. I chose bubble sort as something simple to start with. The first couple of days weren't very interesting but after a while I noticed that the order of tests could make things easier or harder and the implementation decisions could dramatically change the direction of the final code.

Whenever I start a new project I always have to look up the documentation for uploading the initial repo to bit bucket. I read somewhere that if something is difficult you should do it more often. This could be a prime candidate for a kata.

Another idea is in learning a new language. A small set of kata that takes me through all the major syntax might be ideal for the memory work that goes with a new language.

In summary, I feel that I have just discovered something very valuable. It's a technique that I could have been using for years but I dismissed it based on its name. There's a valuable lesson on there for me. If you haven't already checked out code katas then it might be worth looking into.

2 Comments On This Entry

Page 1 of 1


15 September 2014 - 09:17 AM
Interesting.. I would never thought about doing code katas.. maybe have a 'logic problem of the day' tear off calendar..

Some folks would disagree with the benefit.


15 September 2014 - 09:57 AM
Yeah, a month ago I would have agreed with him too. The crux if his argument is that doing the same thing over and over is pointless. It's the same point as those who think kata in martial arts is a waste of time but the misconception is that kata is a mindless practice.

To get benefit from a kata you have to do it with a purpose. John Sonmez suggests adding constraints such as time or line count. That's great but I think he is missing out on the introspective insight from focusing on a small aspect. At the moment I'm focussing on improving my test driven development. By using a problem I'm familiar with, one that I don't really have to think about to solve, I get to focus all my attention on TDD which is something I could do with improving.

When I get around to my bit bucket kata, the project will be a "hello world" app. The point is to brush up on the mechanics of creating and uploading a project so I don't want other distractions.

I certainly agree with his point that completing real projects is critically important. If I were to choose between projects and katas, if I had to choose just one, then choosing projects would be a no-brainer. However, at the time of writing this blog, I had been doing projects for a very long time but had only just discovered kata. I had seen some benefit from adding this new trick into the mix and wanted to share.

To be honest, I think we are in agreement for the most part. The disconnect is in the definition of kata.
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

May 2022

151617 18 192021


    Recent Entries

    Recent Comments

    Search My Blog

    1 user(s) viewing

    1 Guests
    0 member(s)
    0 anonymous member(s)