January 13, 2014
Today was my first “official” day as a novice apprentice at 8th Light. After spending some time setting up vim to my liking, I started working on my first code katas. I had heard about code katas before, especially in relation to 8th Light, but I had never tried one, nor did I ever really understand their purpose until today.
Coin Change Kata
The first kata I worked on today was the Coin Change Kata. The goal is to create a function/method that can accept an integer and dispense the appropriate number of coins (quarters, dimes, nickels, pennies) as change. So, for example:
CoinChanger.make_change_for(45) # => [25,10,10]
CoinChanger.make_change_for(67) # => [25,25,10,5,1,1]
make_change_for(total) method isn’t all that complicated:
change = Array.new
coins = [25,10,5,1]
coins.each do |coin|
while total >= coin
change << coin
total -= coin
All it’s doing is giving out as many quarters as it can without going over the total, followed by dimes, nickels, and pennies. It’s so simple, in fact, that it isn’t too outrageous to suggest that one might be able to come up with this solution right off the bat. However, that goes against perhaps the most important aspect of katas.
Much like the martial arts katas from which the idea is derived, code katas are about going through fundamental motions or steps correctly. I can see how katas will help put me in the proper mindset for Test Driven Development (TDD). To correctly perform the kata, I start by making the most basic test possible:
it "returns one penny when the total = 1" do
You then write only enough code to make this test pass:
Yes, that’s right—the method currently always returns an array
. That is, after all, the bare minimum needed to make this test pass. From here, you add tests one at a time to grow the method. I won’t give away how the kata evolves (I may have given too much away already). The point is that the kata helps put me in the appropriate state of mind to approach problem solving this way. I’ve already received great resources from 8th Light about TDD and why it is so valuable—this article is perhaps the most persuasive I’ve read so far. By performing a relatively simple kata like this, I practice good TDD habits and start to get into the red/green/refactor “flow.”
Again, like martial arts katas, code katas can be performed. I watched a video of Uncle Bob performing the Prime Factors Kata (the second kata I started working on today) and I have to say, it was very cool. Watching the code develop before my eyes in a fluid manner, with beautiful music complementing it in the background, made me appreciate the “magic” and elegance of a great solution devised with code. As Uncle Bob has described himself, this kata is fascinating because as the algorithm becomes more complex, the code actually tightens up and becomes more concise. At the same time, the code transforms from specific to generic. Very, very cool to watch this unfold.
Katas help develop solid TDD habits, and can even be fun to watch.