Yesterday I did the Gilded Rose refactoring kata in Clojure which I started working on in the last Clojure Developers Barcelona meetup.
This is the original code that I got from a Mike Jansen's GitHub repository:
First of all I wrote tests following the description of the kata and had to fix several bugs related with items quality inferior and superior limits, 0 and 50 respectively.
These are the tests using Midje:
Once all the tests were in place, I started the refactoring by improving the indentation and using destructuring on the item parameter to eliminate duplication and make the code read better. Then I started to work on the cond branches working my way to have a separated branch for each type of item.
Every time I separated a branch for a type of item, I created a query helper to make the branch predicate more readable.
Once I had a branch for each type of item, I created helpers to increase, decrease and set to zero the quality of an item.
Finally, I removed the destructuring of item that I had introduced at the beginning, since it wasn't necessary any more and created some other helper functions.
This is the resulting code:
You can follow the whole process I've just described having a look at the commits I did after every small refactoring (look at commits between Gilded Rose original code and Introduced explaining helper)
This version of update-item-quality is much more readable that the original one.
In the next post, I'll replace the conditional logic with polymorphism using Clojure multimethods.
This is the first post in a series of posts about the Gilded Rose kata in Clojure: