Uncharted Waters

Apr 25 2017   8:43AM GMT

Free Kata Tool – And How To Use Them

Matt Heusser Matt Heusser Profile: Matt Heusser


Karate KataWe don’t do deliberate practice enough as programmers. Instead, too much of our time is spent trying to get the next piece of work done. Perhaps we’ll clean it up tomorrow. Perhaps we’ll do it right next time.

Perhaps monkeys will fly.

Instead of trying to clean it up later, I recommend developing skill outside of production code. The Kata is one way to do that. CodeKata.com describes them this way:

“A kata is an exercise in karate where you repeat a form many, many times, making little improvements in each. The intent behind code kata is similar. Each is a short exercise (perhaps 30 minutes to an hour long).”

Today I’ll talk a little about Katas in Ruby, how I set them up, what they are good for – and the kata generator tool I wrote.

Getting Started With Katas

I’ve written a few programs in Ruby over the years, but the code itself was not in my muscle memory. I had to look things up, google search, or use StackOverflow to figure out how to do this or that. My goal was to do the same type of things, over and over, improving along the way.

Now I did not expect to type in the exact same program each time. Instead, I wanted to get better at it, saving time, creating better objects that were easier to use, and so on.

There are two popular katas that I “got into” – the Bowling and the Roman Numerals. Both of these take some kind of input and transform it into a score. In Bowling, it is the scores from ten frames. In Roman Numeral, the Roman number is turned into an Arabic number.

I started by creating an object to do the processing and putting it in a lib directory. The “main routine” of the program includes the object-file, creates a new one, gets the value to transform from somewhere (like the command-line), runs the transformation, and prints out the output. Because it is in a code library, I can also create a test file that exercises the object, and use that to do test driven development.

Creating the shell of the programs, then, gets to be a bit repetitive. Create a new directory called kata_name, create sub directories for lib and test, create the object-name library, create the test file, create the main file. The main file includes the library, creates the object, gets some input, calls run on the object, gets the output back, and prints it out.

The Kata Generator

kata generatorEventually my programmer mind, which is lazy but intelligent, took over, and I created KataGenerator.rb, a ruby program that takes the name of the kata as input and does everything listed above.

The kata generator is dead simple. It literally calls the operating system to create the directories, stores all the text of the files in the program itself, adds the name of the kata, then writes the files to disk. There are so many other things it could or should do. For example, it could store the files in a directory tree, like a template, copy them over to the new directory, and just do a search-and-replace of a specific tag with the name of the kata. This is a one-line sed command.

This is the beauty of katas. Having the input defined up front, you can do them over and over again to learn better ways to do them. I have two implementations of the bowling kata in git; an object-oriented one an a “simple” one implemented with arrays to store the scores. It turns out that scoring a simple game of bowling with a for loop is … not that simple.

For more on my katas, and a description of how to use the generator, check out the main page and scroll down.

In two weeks I will be at Agile And Beyond, in Ann Arbor Michigan. Yes, I have a session to run. Yes, I’ll be running lean coffee. The rest of the time, I might just be hacking in a corner, working on the webserver I am running. Yes, that is correct, I am implementing a webserver.

Let’s write some code together.

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: