I have commands classes that implement ICommand { Execute } interface. Several commands have duplicate pieces of code. I have several options how to DRY:
- Create static helper class(es) and move duplicate code there
- Create commands inheritance with protected helper methods
What would you suggest and why?
ADDED
Thank you everyone who replied, many answers were alike and useful!
It completely depends on the nature of your duplicated code.
What are the inputs / outputs of you helper functions? Do they operate on a logically related set of variables? Then – yes, you’d better create a base class with those variables as members, and related set of helper functions.
Otherwise, if the parameters in your helper functions are not coherent, you would implement those functions as static functions anyway, right? I don’t see reasons to complicate things with inheritance in this case and I would do it just with helper functions (or, if your language doesn’t treat functions as first class citizens, use static helper class).