String Calculator Kata (OOP)

_config.yml

Picture attribution

Some how – I stumbled upon Roy Osherove’s katas. And so I warmed up my Visual Studio editor and set up Nunit and started testing and refactoring my way to success. It turns out, yet again, that this problem is exactly the same as all the previous problems I’ve encountered. Perhaps it’s because my brain is seeing the same pattern and applying the most logical solution.

See Roy’s blog here for instructions::

  1. Delimiters can be of any length with the following format: “//[delimiter]\n” for example: “//[]\n12***3” should return 6
  2. Allow multiple delimiters like this: “//[delim1][delim2]\n” for example “//[][%]\n12%3” should return 6.
  3. make sure you can also handle multiple delimiters with length longer than one char

I got to #7 but my regexing was a little weak and the language itself is cryptic and hard to decipher. There’s gotta be a new idiot proof syntax/language built around regex so that people can more easily understand it rather than using arcane escape symbols. It would be an extremely successful project. A syntax something like Match.CharacterClass.All.Except.LiteralCharacters(“#”) etc. I didn’t want to spend another 20 minutes scrambling to get this regex to work. But we can see the solution: either we can handle the regex in the existing class. If not we can create a new subclass to handle these types of situations. Either way the problem is still the same. Every problem since 99 bottles is no different – the only exception (pun intended) was the Tower of Hanoi, where that was a recursive solution.

The factory method is used to choose an implementation. And that particular implementation: adds the strings. It’s very, very simple.

Here is the full code (please see below). There is a minor bit of duplication, but rather than create a new superclass, I don’t mind the duplication. If further requirements come in, then we might be able to create a super class for the Complex and Simple SmartString classes, but till that time I am happy to tolerate the extra duplication. It’s quite an elegant solution and I hope you learn something, or at least, open your mind to the possibilities of approaching the problem slightly differently to what you were doing before. I’ve used a base class to construct an interface – yes, yes – I hear you – I could have used an interface type but I don’t particularly want to duplicate functionality when it can exist peacefully in a base class without causing much trouble.

Here is the full code:

And here are my tests:

The positives:

  • It’s organised and very readable. This is key.

  • I adopted a polymorphic approach. Notice how the conditionals and the actual behaviour are separated?

The negatives:

  • Regex is used (it’s not easy to understand).

  • A little bit of duplication – but it certainly is justifiable. Furthermore, I didn’t want to complicate things by creating another base class of an abstract class to eliminate this slight duplication.

I hope this is making some sort of sense.

Written on April 28, 2017