samedi 24 janvier 2015

refactor by inheritance or composition for large class that depend on common state


I have a huge class (>1000 lines), in which all methods depend on a small set of attributes in that class. I can think of 2 quick ways of refactoring.


1) I keep it as a huge object, but split related methods into separate classes from which I inherit (but I don't instantiate those separate classes, only the last one in the inheritance-chain). This probably has the disadvantage that conceptually, my object is still a huge monolith.


2) I make the common attributes, with a few helper functions, into a separate class. All other code that depends on this common state then are in their own class, but contain the common state through composition. Disadvantage is I end up with a lot of nested lookup calls similar to self.commonState.foo, instead of just self.foo.


The latter is getting a bit reminiscent of the MVC pattern of course. Is there a clear answer to which method is preferred?





Aucun commentaire:

Enregistrer un commentaire