Home / .NET / The fluent calculator kata

The fluent calculator kata



We are currently preparing for the next coding dojo in our team. A team mate and I came up with a nice kata which we are going to try out today. We didn’t check if there is already such a kata available on the internet. So please take no offense if you already came up with a similar solution ;)

  • All input values should be int.
  • The calculator should never throw exceptions.
  • The api should guide through the fluent interface, i.e. after the calc method you are never again able to call the calc method.
  • It is possible to get the result by implicitly casting it to int.

Here are two examples:

int result = Calculator
		.Calc(10)
		.Plus(5)
		.Minus(2)
		.Undo()
		.Redo()
		.Undo()
		.Plus(5);

// -> result = 20

And another one:

int result = Calculator
		.Calc(10)
		.Plus(5)
		.Minus(2)
		.Undo()
		.Undo() // -> 10
		.Redo()
		.Redo() // -> 13
		.Redo();

// -> result = 13

For fast teams there is another requirement which extends the calculator:

  • It is possible to call Save anytime which saves the state of the previous operations
  • After a save point directly executed Undo or Redo operations have no effect
int result = Calculator
		.Calc(10)
		.Plus(5)
		.Minus(2)
		.Save()
		.Undo()
		.Redo()
		.Undo()
		.Plus(5);

// -> result = 18

We appreciate feedback!