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

Testing Categories in Objective-C

Yesterday was katas day: the plan was to start the day with a simple one and then move to another a little more difficult. That was a good plan.
So I decided to do the Stack: a very simple kata, I just had to simulate a stack and have the following methods:

And for rules just this one: “stack should throw an exception if popped when empty.”

Simple right? I thought so too. And it was really easy to solve it: just create a NSMutableArray category and add the methods to NSMutableArray.

So I create I first test :

So I create my class and my code and everything builds! Great, just run the tests and that’s it! No it isn’t!!! Here’s the result of my test:

What? So the code builds and then I have this error:”unrecognized selector sent to instance”. Well let’s ask google….
In reality it took me all morning to figure it out! I must improve my search techniques!!! After googling a lot and a lot of frustration, I find a blog post that saved my day: So if you want to test a category you must add -all_load flag to your test project. Go to: YourTestTarget > Build Settings > Linking > Other Linker Flags.

Problem solved!
You can find my final solution here.