How katas can help you learn

Baton_long by Alain Delmas

In the last couple of weeks I have spent my time doing katas to improve my Objective-C. As you may remember, I had an issue, that you can revisit here.

I’ve learned a lot from other katas too: for instance the RomanNumerals kata… You’d may think it’s a simple task, that you wan’t learn much from. It’s a simple algorithm, a good exercise for your red -> green -> refactoring cycle. So nothing new will come from here… Wasn’t I wrong!!!!!

First I created some tests. Next I implemented the kata and refactored. Whilst I was refactoring, I decided to use NSDictionary to map from a decimal to a roman number. Here is an example:

That’s when I discovered that the NSDictionary does not guarantee insertion order. What???? So my keys were all mixed up… And I needed them to maintain their order!!!! That’s ok! I decided to create a class to map between decimals and romans. That’s cool! Done! In the converter I just added a private property of type NSMutableArray. In it’s constructor I added the class to the array for all elements I needed. But now I have to do:

for every entry in the mapper. God!!!! So much work! Maybe that’s why developers solve problems. They definitely don’t like to do things by hand so they automate everything.
So I decided that I could create a factory method. I really didn’t know how to create this factory method, so I just looked in Apple docs. Here is another thing i learned. Even thought I knew that it existed, I never used it because I never needed to. But as I was in learning mode, I think I was more interested in finding a different way to do it. When you are at a client, you don’t always have the opportunity to experiment with new things. Having this time to learn new ways of doing things is really rewarding. So here is my class:

It’s even more readable then the initialiser. And here is how I initialised it in my converter:

So my class was looking pretty but then I look at my test class…it wasn’t good:

So many tests. They remind me of the DRY principle. I really don’t like to repeat myself. It’s like a broken CD that doesn’t move from the same music track… I’ve done the same kata in C# and my test class was all parameterised and I really liked it. After all this is a simple kata, right? ;).

Well wouldn’t it be nice if I could have that in Objective-C? Well, after some help from Franzi I found a little library that does it. You install the pod, you inherit from it and you create an array of inputs and expected values like this:

And then you just have to use the properties input and expected. So my test class looks like this:

Here is my final solution: RomanNumerals.

So please don’t underestimate what you can learn from a kata. They are a good opportunity to stretch your knowledge and add some more to it!
image: Baton_long by Alain Delmas

Leave a Comment

Your email address will not be published. Required fields are marked *