Sunday, November 22, 2009

Code Kata

This has been bothering me for a while. I don't like to rant without a solution though. I have one now, so a small rant, and then a solution:

Code Kata, as they're described by Dave Thomas, are NOT really kata in any traditional usage of the term. He sums it up fairly accurately:
Kata (Japanese for form or pattern) are an exercise where the novice repeatedly tries to emulate a master. In karate, these kata are a sequence of basic moves (kicks, blocks, punches, and so on), strung together in a way that makes sense. You’ll never be attacked in such a way that you could repeat a kata to defend yourself: that isn’t the idea. Instead the idea is to practice the feel and to internalize the moves. (Interestingly, kata are not just used in the martial arts. Calligraphers also learn using kata, copying their masters’ brush strokes.)

Yep. That's the idea PrgDave. A series of repeated moves to get you ready for a potential attack. Just to internalize the motions.

So how has PragDave applied them? Code Kata One. Does this resemble what he described? Is this a series of movements that you would practice over and over to internalize the response to a situation? No, this is asking for you to give your response to a situation. This is the equivalent of saying "How would you handle a straight punch to your coccyx?"

If we REALLY try to keep the martial arts/code study metaphor intact, we have a few different levels to look at:

1. Physics- Basic F=M*A. Knowing the forces at work to land a successful hit or in the case of computers, knowing the cs stuff(algorithms, data structures, time complexity, etc.). A bag that tells you your killing power and benchmarking tools are good, but they won't tell you why. This is essential to being good at either. Naturally, a good fighter doesn't have to be a physicist. She can rely on the integrated experience of past teachers to give her the expertise. A good programmer should know at least enough to look it up, but depending on the problem, a lot of the hard questions are abstracted away in the environment (ie. garbage collection is rarely something you need to think about when developing a basic Rails app). More on this in a later post.

2. Biology- Do you have both arms and legs? Are you fat? Do you have a trick knee? Do you have all of the packages installed? Which implementation are you using? Are you on Windows?(I won't say which one this is)

Ok. So as far as applications:

3. Fight- A feature to implement, a bug to fix. This may be a grouped collection of bugs and features to deal with.

4. War- Your job. A stream of fights which may or may not be easy.

Practice:

5. Pair work- Ruby quiz, PragDave's code kata. This is when you have a problem, and have to figure out the solution. This is a mock fight when you know the attacks, and have to invent the response. I'm not suggesting that this isn't an essential part of training, but it is not the same as practicing "code kata."

6. Kata- Practicing the moves that you will need to do automatically. Basically, this is what you to cache in your brain as opposed to having to look up the API. So... what does a kata look like? public static void main {
}
It looks like that. That is the first thing I learned about java. I didn't know what it was in response to, or what it was actually doing. I just knew that I had to memorize this for usage in nearly every fight I would encounter.

A suggested kata for ruby would be something that practices basic motions that you're likely to forget but need to use frequently. These are naturally going to be somewhat specific to a person, but to develop your own, keep track of what you look up. If you find yourself looking up the same thing very often, work out an exercise that uses that code. Alternatively, develop kata that use features that you don't use, but would be useful if you did.

Ruby's hash, array, and string classes are good places to start. contains, in_array, include and all of the question mark variants are all possibilities to find out whether an array contains the element. Which one is it? What does it return? Are there hash and string variants? Are there other methods that cover most use cases better than include?

But isn't this what "tutorials" and "recipes" are for? Yes. It is for the most part. However, most "recipes" are a little too coarsely grained. Most "tutorials" cover only the basics. Neither emphasize caching your knowledge to prevent expensive lookups to google, wikipedia, the reference itself(recipe book or blog post) or a person later on.

What should and shouldn't be cached/known is a topic for another post.

No comments:

Post a Comment