A different kind of kata: Harry Potter books
Join the DZone community and get the full member experience.Join For Free
During a day off I stumbled upon a kata that had a simple definition: the Harry Potter books kata. It is old enough that it considers 5 as the total number of Hary Potter books instead of 7, but it has aged well.
Unlike for the bowling kata, there is nothing specific to Harry Potter in the problem. The domain is that of e-commerce and the problem consists in calculating the discount a given set of books is eligible for; for example, considering every copy has a list price of 8 pounds, how much a purchasing 2 Philosopher Stone books and 1 Chamber of Secrets would cost.
I don't want to talk much about the problem itself here to avoid spoilers for you; suffice to say that it took me 8 hours to get to a solution that I could be proud of (the first couple of which to get to a potentially shippable solution, still not being confident of it being bugs-free or of its extensibility.)
I want instead to enumerate some of the traits of the problem that make it interesting with respect to the more popular katas. Some of these traits could even invalidate the problem as a kata; still it's a challenging exercise.
First of all, the Harry Potter kata does not have a trivial solution. Some katas focus on learning a new technique like TDD or how to follow the Open/Closed Principle; they keep the related problem simple enough not to change two different variables of the experiment at the same time.
Still, most programmers that take the time to perform katas can see the solutions to these problems once they have read it. They can in principle write a long procedure that will solve the problem, satisfying functional requirements and probably not the non-functional ones such as maintainability or extensibility.
Following a discipline even when you see an easy solution is probably a useful skill; however I feel that too simple problems prime the head of a programmer with the first design he can think of, so that applying TDD becomes a way of putting the design into practice instead of let it emerge bit by bit while adding new specifications.
Instead, consider a problem which is just out of your reach. You can's solve it in a single shot and when you take a look at the problem you see some statements that can make you panic: "I don't know how to do that... yet". This problem is a much better simulation of real world conditions. It forces you to add assumptions to simplify the problem and follow an incremental path towards the solution instead of adding now components that you will only use later, if only to keep the cognitive load manageable.
The Harry Potter kata divides nicely into almost-independent test scenarios. The input and output state are simple to express and the same API used for the first test can be used without much expansion for the last ones.
For example, the first test comprehends 0 or 1 book. Intermediate ones get to 4 or 5 books some of which are duplicated copies with the same title. Large acceptance tests get to more than 20 books (there's a reason for that.)
I can't say too much about the steps I took to solve the problem without ruining your fun.
However here are some beliefs I think are challenged by the experience:
- One new test case does not result in wild modifications to the production code.
- You can follow a monotonic path to the solution: you don't ever have to abandon functionality that you have already written when refactoring.
- Code organization and documentation are really a secondary aspect as code can be self-documenting.
- Once you have added a few corner cases you don't have to worry about the code working even if the solution is difficult to understand.
- Worry about efficiency later. First make it work.
The Harry Potter kata is a nice and challenging exercise, in the category that keeps you glued to the keyboard for a long time. It's definitely more time-consuming than smaller katas, like for the application katas, so you have to allocate a sensible amount of time to solve it. However, the benefits I feel from this kind of training are different from the ones of toy problems: problem-solving is a different art than training a particular practice.
Opinions expressed by DZone contributors are their own.