This well-known article criticises Java on the basis that it does not allow you to write functions that do not live in a class. This flaw forces you to write classes with names that look suspiciously like verbs, such as GridCellHander
or GarbageDisposalDestinationLocator
. In other words, it looks like you've built a class (i.e. a noun) to escort these methods (i.e. verbs). I believe that the article is entirely correct and I feel disturbed when I write a class with a name similar to these, particularly in a language that does not have Java's restrictions.
However, it is utterly standard advice that if you have a small amount of methods within a class that are all the only users of some of the class's state, then those methods and their shared state should all be placed in a different class. This maintains cohesion and keeps your classes small.
How can this advice be followed without writing an "escort" class that clearly only exists to hold these methods?
For example, I'm currently dealing with a grid. Some of its cells represent times. These times need to be sent to my server. I originally had two independent methods for handling them, one for deleting from the server and one for upserting. When I realised that they shared state, I put them and their shared state in a TimeCellHandler
class. This let me significantly simplify the parameter list of the methods and let me write helper methods that they share. This follows the guidance described in my second paragraph, but violates the first. I can't just call this class TimeCell
, because it plainly doesn't represent those cells; It only represents the small subset of their data needed to talk with the server about them.
Note that this question is language agnostic. The article linked gives the impression that functional languages (e.g. Scheme) can always replace TimeCellHandler
with something better, but I'm just as happy to see an OOP or non-OOP answer. I don't care if you have to resort to CLOS or macros. I will be happy with any answer that demonstrates how to group two methods that share state in a sensible way that does not involve naming a class (or your language's most similar concept) in such a way that it makes it obvious that the class only exists to hold the methods.