Monthly Archives: March 2012
In my last post, I introduced the concept of a code-kata (coding exercises) and gave an example problem to solve:
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
For those of you who tried the example, how did you find it? Pretty simple right?
Did you make it look this easy though? Whoever recorded that video is nicely demonstrating the benefits of honing their development environment – he/she even goes as far as disabling their mouse to train themselves to use the keyboard more effectively.
How well was your code designed? Is it adaptable to change?
Let’s move the goal posts
In real life, requirements change all the time. We can easily practice writing adaptable code by throwing some new requirements into the mix.
Try to take these steps one by one, as if a client was drip feeding them to you – try not to read ahead too far!
- Try extending the application to support another range of numbers, such as 15-175
- Try extending the application such that a user could provide any range of numbers (such as from the console, or from configuration)
- Try extending the application to support new rules – output “Baz” for numbers divisible by 4
- Instead of printing the numbers to the console, try extending the app to write to a file
Put your money where your mouth is, show me the codez!
Ok, my C# kata can be found on my github page here: https://github.com/CraigCav/FizzBuzz. Over time, perhaps we’ll see more implementations of this appear on my github page in other languages, such as F#. Interestingly enough, I used this example to hone my environment; to learn the GIT commands (instead of using a GUI) by practicing them over and over with this small simple example.
The concept of a code-kata is a simple one and its premise is borrowed from its martial arts counter part:
Kata (型 or 形 literally: form) is a Japanese word describing detailed choreographed patterns of movements practiced either solo or in pairs.
In a code-kata, we practice coding problems to train our minds muscle-memory such that when faced with real-world coding problems, the solutions are at the forefront of our mind.
The general idea is to practice solving problems in new ways, new languages, or in new environments. By practicing techniques that are just outside of our comfort zone, we can push ourselves to learn. Additionally, we can use this same approach to tune our environment in an observable manner in order to reduce any friction in the way we are working.
I came across the FizzBuzz code-kata a few years back and I’ve seen a few slightly different versions of it in my travels. In fact, this “problem” even came up in an interview I had once, so practicing the kata definitely paid off!
Here’s the FizzBuzz problem description:
Imagine the scene. You are eleven years old, and in the five minutes before the end of the lesson, your math teacher decides he should make his class more "fun" by introducing a "game". He explains that he is going to point at each pupil in turn and ask them to say the next number in sequence, starting from one. The "fun" part is that if the number is divisible by three, you instead say "Fizz" and if it is divisible by five you say "Buzz". So now your math teacher is pointing at all of your classmates in turn, and they happily shout "one!", "two!", "Fizz!", "four!", "Buzz!"… until he very deliberately points at you, fixing you with a steely gaze… time stands still, your mouth dries up, your palms become sweatier and sweatier until you finally manage to croak "Fizz!". Doom is avoided, and the pointing finger moves on.
So of course in order to avoid embarrassment in front of your whole class, you have to get the full list printed out so you know what to say. Your class has about 33 pupils and he might go round three times before the bell rings for break time. Next math lesson is on Thursday. Get coding!
Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".
This Kata is best used to introduce the concepts of Test-Driven Development; Red-Green-Refactor! If you’re new to TDD, or even if you’re just looking to hone your technique, this is a neat little example to try, and there are dozens (if not more) ways to solve this problem.
Write your first test, watch it fail, fill out the implementation, make it pass, then clean up (refactor).
In my next post, I’ll spice it up a little. If you haven’t tried the Kata yet, don’t peek - I don’t want to spoil your fun!