Blog

imageIn Coding Katas mostly programming tasks are set that are intended to solve a specific problem on a green field. I often experience that participants of the katas can easily adopt these micro-tasks into their projects. However, they often find it difficult when it comes to integrating existing solutions where they have to mock in order not to have to fully initialize the existing components.

In real projects, you often encounter situations where you depend on external dependencies and have to serve and use their interfaces. Unfortunately, this approach has been neglected in many coding kata examples so far. In this article, I would like to explain a task that I use for this purpose in my coding dojos at team neusta.

The idea is not exclusively my own. I thank my colleagues Frank Supper and Sebastian Foot for the initial idea and Sebastian Bock while writing this article.

The basis of the task is the String Calculator Kata, with the following interface:

public function add(string $numbers): string;

The requirements for the $numbers parameter come from the StringCalculator Kata:

The method can take 0, 1 or 2 numbers separated by comma, and returns their sum. An empty string will return “0”. Example of inputs: “”, “1”, “2,3”.

Create a class Calculator that masters the four basic arithmetic operations (addition, subtraction, multiplication & division), as these can all be represented mathematically via addition.

The interface of the Calculator class is:

public function calculate(string $arithmeticTask): float;

The format of the $arithmeticTask parameter should look as follows:

  • 1+2

  • 5-2

  • 3*5

  • 15/3

    Spaces do not matter; any number or none can be between the characters.

    The Calculator delegates the calculation to the StringCalculator.

    The StringCalculator has no implementation and should be mapped exclusively as a mock object.

  1. It should be possible to process more than two operands in an $arithmeticTask.

  2. Outsource arithmetic operations from the Calculator class.

  3. It should also be possible to pass float values to the Calculator class to add / subtract / multiply / divide, e.g. 2.5 + 3.5 or 3 * 2.25.

  4. Parenthesizing of arithmetic expressions, e.g. 10*(3+5).

Image source: Wikimedia Commons