Code Katas

The term code kata was coined by Dave Thomas in his book the Pragmatic Programmer and I have always thought it’s a good idea. I trained in Taekwondo for several years and we also had a form of ‘Kata’ called Poomsaes.

Some people used to misinterpret Katas as a waste of time as the moves and sequences are just predetermined and there is no point training like that, as the same scenarios would never play out in a ‘real’ fight or tournament. But anyone who has trained in a traditional martial art like Karate or Taekwondo will know that Katas or Poomses are there so you can practise each individual move to perfection and this serves to form a muscle memory of the individual moves and sequences so that they come naturally in a more real world scenario.

I latched on to the idea of code katas as it seemed like a great way to practise programming without embarking on a full blown hobby project. It’s hard for an enterprise developer to justify writing code that is just thrown away and that you won’t get paid for ‘just for the sake of it’ but finding time to get back to the basics definitely pays off and keeps you sharp. The good thing about code katas are that it keeps your basic skills grounded and forces you to remember how to do the basic stuff that you would probably never use in your main job (until one day you do).

I wish I did more code katas as like anything practising something makes you better, you can’t get around that fact. The only problem is finding the time or motivation when you have a deadline or are juggling other commitments.

I myself keep a Visual Studio solution containing lots of different projects that I use to practice different concepts or algorithms, the code in the projects isn’t great it won’t conform to best practices or be very elegant but I treat it like my interactive notepad of code.

I have uploaded the project to Github so anyone can take a look.

https://github.com/leedale1981/CodeKata

An example one of the projects is a simple string reversal in C#, this allows me to test out how different techniques perform next to each other, for example I tested string reversal in C# using the LINQ Reverse() method and a basic char array reversal using a for loop. I found that the basic for loop array method was orders of magnitude faster than LINQ. This was expected but it’s nice to prove to yourself so you know in future which is more the performant technique.


class ReverseString
{
    public static void Main(string[] args)
    {
        string input = "reverse me";
        Stopwatch timer = new Stopwatch();

        timer.Start();
        string outputLinq = UsingLinq(input);
        timer.Stop();
        Console.WriteLine("Using LINQ: " + outputLinq + " - time taken: " + timer.Elapsed.ToString());
        timer.Reset();

        timer.Start();
        string outputArrayOnly = UsingArrayOnly(input);
        timer.Stop();
        Console.WriteLine("Using array only: " + outputArrayOnly + " - time taken: " + timer.Elapsed.ToString());
        timer.Reset();

        Console.ReadLine();
    }

    private static string UsingLinq(string input)
    {
        char[] output = input.Reverse().ToArray();
        return string.Join(string.Empty, output);
    }

    private static string UsingArrayOnly(string input)
    {
        char[] inputArray = input.ToCharArray();
        string output = string.Empty;

        for (int index = inputArray.Length -1; index >= 0 ; index--)
        {
            output += inputArray[index];
        }

        return output;
    }
}