Programming

my faceChris Foley is a computer programming enthusiast. He loves exploring new programming languages and crafting nice solutions. He lives in Glasgow, Scotland and works as a software developer.

A journey through Code Katas

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: roman numerals kata.

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 (Uncle 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 them, 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 there for me. If you haven't already checked out code katas then it might be worth looking into.

15 September 2014

Comments