Field formatting methods is an antipattern

Some years back I wrote a conceptual small class. This class is central to our processing of data and have over the years been extended where needed to fit many different coding projects (20+). Looking at this class and reflecting about possible improvements to be done, it is clear that having different methods that returns certain information X in different output formats can be improved by encapsulating this common behaviour in a new class. In other words, move all those methods into a class representing X and make a method in original class to return X.

For instance, you got a Subscription class representing a from date and a to date, among other stuff. You can improve by making a Period class that contains a number of formatting methods, instead of putting these formatting methods in the Subscription class.

I think it is an antipattern to write field formatting methods. If you need to format anything, it should be the class representation itself, not parts of it.

Kommentarer