I did Emily Bache's Parrot Refactoring Kata again (I did this kata in Java some time ago) but this time in Clojure. This is a very simple kata that is meant to practice the Replace Conditional with Polymorphism refactoring.
This is the initial code of the kata that have to be refactored:
Since Clojure provides several ways of achieving polymorphism, I did two versions of the kata:
- One using protocols:
This refactoring was a bit more involved than the one I did for the next version, but I managed to get rid of the exception and the resulting tests were more readable.
If you feel like following the refactoring process, check the baby steps in the commits. You can find the code in this GitHub repository.
- And another one using multimethods:
This version is much shorter and the refactoring is less involved than the previous one, but the resulting tests read much worse and the map representing the parrot has many "optional" entries.
Again, check the commits of the refactoring baby steps here, if you feel like following the refactoring process. You can find the code of this second version in this GitHub repository.
I'd like to thank Emily Bache for sharing this kata.