Friday, April 26, 2013

Kata

Kata is an exercise which helps students hone their skills through practice and repetition (Wiki). As an apprentice, I have to perform one kata a week for my mentor, and once a month in front of a larger audience.

It's easy to dismiss this exercise because one is always looking to do bigger and newer things. As a karate student, you're often interest in learning how to do a spin kick or other cool things you see in Kung Fu movies. You probably don't know that if you execute a normal kick perfectly, you can injure your opponent in one shot instead of having to do several poorly spin kicks (which waste energy and time). It's been awhile since I took karate, but if I remember correctly, in order to do a perfect kick, you have to:
  • assess the situation which includes your surrounding and your opponent. (You don't want your foot to land on a banana peel after your kick.)
  • figure out your opponent weak spot.
  • execute the kick from your hip and focus your energy on the kick to create the most impact.
  • prepare for your opponent defense/counter attack.
All of these must be done in less than a second. It took me five minutes to remember the steps. In five minutes, my opponent could have beaten me to a pulp. So can anyone actually do this? Yes, all karate masters do this. The masters do all of these steps naturally, without even thinking because they've spent years and years performing the same kick over and over again. Through repetition, the masters have ingrained these steps into their memory so that it comes naturally. They don't even have to think before they execute.They execute perfectly each time, all the time, with very few exceptions. And whenever they don't perform one of these steps, they'll feel a nudge from their conscience telling them something is wrong. In addition, when they learn a new kick, they can focus on learning the movements because all the other steps are ingrained in their memories.

That is what I hope to achieve in code katas. I want all the good programming practices (e.g. write test first, write good variable names) to be ingrained in me so that it becomes second nature. I should feel a nudge whenever I start writing bad code.

The second part to practicing katas is that you often find new ways of doing things. The first time I did the prime factors kata, I was really slow. I struggled to write the test and write the simplest code to make the test pass. I had to watch Robert Martin do the kata, several times, to see how he came to the solution and try to emulate that.

After practicing the kata for a week, I'm beginning to feel its effect on me. I feel a nudge when I write code without a test. I begin to see what kind of test I should create to make the algorithm better. I still haven't perfected my kata yet, not that there is a perfect solution. Programming is an art, as they said. There is no perfect solution. However, I still haven't arrived at the solution I'm satisfied with. So the goal is to practice a kata a day. And only one kata a week, which means I will be doing the same kata at least five times. And every time I do the kata, I have to think of better way to write it.  

0 comments:

Post a Comment