Saturday, May 10, 2014

The Form of Katas

-- Shakespeare, Coriolanus
Act II, Scene 2, Line 142.1

Intro

Every work day around 6:30 AM you'll find me working on a Code Kata.  I find that this is one of my most inventive times of the day.  While doing my Code Kata I try out everything and anything.  I am truly free doing my Code Kata, I even allow myself to fail.

Around 6:55 AM my kata be it is good, bad, or some where in between comes to an end.  I get up and go make some coffee to start my work day.  (In case you are wondering, my kata takes place out side of my normal 8+ hour work day.  I write this since it has come up in talking with people at conferences about katas.)

Why do I do my Code Kata?  I have lots of reasons, but the main ones are the following:

1) I do a Code Kata everyday to get better.
2) I do a Code Kata everyday to try out new things.

I wrote about these in more details in an earlier post called "A Kata a Day".

What is a Code Kata

A Code Kata is simply, focus programming for the joy of programming.  Focus because you are working on getting better.  Joyous because you want to have fun doing it.  (Pro tip, if you do not like programming than why bother doing it?)  That is all.

I typical choose from a few simple katas that I know rather well.  I do this because I do not want the problem domain to get in the way of what I am focusing on.  What does this mean?  If I want to try out a new JavaScript framework then I do not want the problem domain of my kata to get in the way of learning how to use the framework.

FizzBuzz

The FizzBuzz kata is a very simple exercise yet has enough to it to be usable to vet candidates at an interview (we use this where I work to vet our candidates).

Examples

FizzBuzz(2) -> “2”
FizzBuzz(3) -> “Fizz”
FizzBuzz(5) -> “Buzz”
FizzBuzz(15) -> “FizzBuzz”

Rules

• If value is divisible by 3 then Fizz
• If value is divisible by 5 then Buzz
• If neither then give value as string

As you see you have one edge case with values which are divisible by both 3 and 5.  In fact one such edge case is the value 0 which cause issues in some languages like JavaScript in which 0 is a special value.  The great thing about FizzBuzz is that you can use it to try out new ideas like creating a RESTful FizzBuzz API.  Yep, FizzBuzz as a service!

Check out this slideshare from the most recent meeting of the Lake County .Net Users Group.  (Yeah I know I am a Co-Leader of said group and that link is to my Slideshare!  If I do not promote myself than who will?)

A Solution

Here is a solution using C# with LINQ.

Coin Changer

I first saw the Coin Changer kata at SCNA in a battle kata.  This is a great kata since you get to work with a collection of data.

Example

Changer.coins = [pennies]
Changer.for(3) -> [3]

Changer.coins = [dimes, nickels, pennies]
Changer.for(17) -> [1, 1, 2]

Changer.coins = [quarters, dimes, nickels, pennies]
Changer.for(99) -> [3, 2, 0, 4]

Changer.coins = [nickels, pennies]
Changer.for(99) -> [19, 4]

Rules

• Given coins of different values
• Find the number of coins given back for a given amount

This kata has a lot of interesting edge cases to it.  Like the following edge case, what should you do if you have only dimes and nickels but need to make change for 36 cents?

Again, check out this slideshare.

A Solution

Here is a solution using C# with LINQ.

Other Ideas for Your Next Code Katas

Looking for new ideas for your next kata?  Check out the following:

Happy coding.