Dear fellow Clean Coders,
I have a question about choosing between two possible designs.
There are some code katas that doesn't specify whether the expected functionality should be implemented in a separate class (requiring to create a new instance of the class to run the functionality) or in a simple function (or a static method in a language where you have to have a class).
When practicing TDD, you first think of the API, how you would like to use the code. And it is easier and more convenient to only have to call a single function, like
const wrapped = wrap(input); // or static method call like Wrapper.wrap(input)
instead of
const wrapper = new Wrapper();
const wrapped = wrapper.wrap(input); // or pass input into constructor
On the other hand, if you have a class, and an instance of it, then you can have all the input arguments and intermediate values on the 'this' context. That makes it easier to refactor, to extract functions without having to pass all the necessary arguments (however, some say accessing and manipulating 'this' makes it harder to understand for them, as the method becomes impure, having side effects).
A lot of the times I end up exposing a simple function on the API that the tests/clients can just call, and I create a class for only to instantiate and call from the exposed function. That class is for that private use only, and usually becomes a 'command-like' class, since it is instantiated and executed every time it is used.
What are your heuristics deciding whether a class or just a simple function? Are there other options? How do you choose the design? What other aspects needs to be taken into consideration?
Cheers,
Gabor