Who's Afraid of the Big Bad Class?
Join the DZone community and get the full member experience.Join For Free
ferocious, guzzling black holes of the software universe, super-massive classes result from run-away positive feedback: the class grows so huge that programmers fear any attempt at refactoring and instead simply dump more functionality into it, thereby making it even more likely that the next programmer will also pale before the terrifying refactoring and jettison yet more functionality into the maw. all stare at the hypnotizing accretion disc knowing that this class must be destroyed, its functionality scattered to a host of smaller classes, but no one knows where to start. which methods should be moved first? how should this choice be made? what criteria might aid the decision?
even the revered admit vacillation. fowler, in his fantastic refactoring book, writes the puzzling, "if i am not sure whether to move a method, i go on to look at other methods. moving other methods often makes the decision easier. sometimes the decision is still hard to make. actually it is then no big deal. if it is difficult to make the decision, it probably does not matter that much. then i choose according to instinct; after all, i can always change it again later."
this rather rapid resorting to instinct seems regrettable when several objective engineering criteria stand at hand eager to assist.
take figure 1, for example, showing a spoiklin diagram - in which each circle represents a method, each line a dependency - of the sequencediagram class of (alas) spoiklin soice itself.
figure 1: class sequencediagram.
this class has gorged itself fat, an unhappy state which calls for the creation of a new class into which some of sequencediagram 's functionality may move. where should a programmer start in the search for methods to move? four criteria help.
first, the refactoring aims to strike some balance between the donor class and the new class. there seems little point in moving most of the methods from the donor class into the new class as this just transports the complexity from one class to another, solving nothing.
second, the new class should have integrity in and of itself. it would not do to move randomly chosen and unconnected methods into the new class: its methods must serve some common purpose. shared dependencies offering a strong indication of shared responsibilities, the methods of the new class should form an inter-dependent unit, a goal best achieved by selecting sets of already collaborating methods from the donor class. thus the programmer seeks not to move methods ad-hoc but to move sets of dependent methods.
third, the migration of any candidate set of methods should cause minimal disruption to the rest of the system. modern tools and ides greatly ease the mechanics of moving methods but only the programmer can protect the design left behind from undue mutilation; just as the new classes must enjoy integrity so too must the donor stump. the number of methods that depend on any of those in the candidate set offers an indication of the degree of disruption caused by the refactoring. this number shall have a strong say in candidacy appropriateness.
armed with these three criteria (the fourth will be encountered shortly), the programmer can approach the search systematically and objectively. the criteria do not guarantee a solution, but they at least avoid premature resignation. in the diagrams that follow - spoiklin soice's, "encapsulatable," analysis - impacted sets will be investigated, an impacted set being the set of methods touched by all transitive dependencies from any particular method. each method's circle will be coloured red according to the number of dependencies on that method's impacted set: a deep-red circle indicates a method on whose children (and grandchildren, etc.) many dependencies fall suggesting an unsuitability for relocation. pale-red circles, on the other hand, indicate methods on whose descendants few dependencies fall and thus suggest excellent candidates for transportation. the tool-tip will give the actual number of these dependent methods. (black circles represent methods on which fewer than two dependencies exist.)
in figure 2 below, the programmer has clicked-on the execute() method to highlight its impacted set (again: the methods on which it transitively depends) and found that only five other methods depend on elements of this entire set.
figure 2: probing for movable methods: too many.
this selection, however, fails because of the first criterion: moving this large a set of methods to the new class might make the new class almost as large and complicated as the donor. undeterred, the programmer probes further, clicking on the drawdiagram() method in figure 3 below.
figure 3: probing for movable methods: still too many.
unfortunately, here again the impacted set of the drawdiagram() method appears too bulky to balance the behaviors between the two classes and it, too, must be rejected. the drawclasslines() method then sweeps into the cross-hairs, see figure 4.
figure 4: probing for movable methods: too much impact.
here the set of methods to move looks, at first, ideal, the set being neither too large nor too small. this selection is only scuppered by the number in the tool-tip: twelve other methods depend on various methods of this set, rendering it too disruptive an extraction (the method's deep red colour should have been a warning). the search proceeds. in figure 5, below, the programmer clicks on the drawlinefromcallingtocalled() method.
figure 5: probing for movable methods: a suitable candidate.
success. this selection offers a set of six methods all serving the same master, on whom only six other methods depend. this strikes a good balance between set-size and disruption and makes a good candidate for a first refactoring. figure 6 below shows the refactoring in action, with each of the six methods yanked in turn from the sequencediagram class.
figure 6: moving six methods.
after this refactoring, the sequencediagram class appears as shown in figure 7.
figure 7: first refactoring results.
note that some extra methods were added to the top line: these were getters necessary for allowing the new class to read some data from sequencediagram which maintains data-ownership; these methods will eventually sink into an interface sloughed off to avoid circular dependencies. being mere getters, they add little complexity to the donor class and thus constitute a worthwhile compromise. for interest's sake, figure 8 shows the new class, callingline , with its proud new tenants.
figure 8: a new class is born: callingline.
the task, of course, remains unfinished. despite a successful operation, the donor class still weighs too much and further method sets must be transplanted. here, the fourth and final criterion comes into play: depth . good design dictates the minimizing of transitive dependency length where feasible. studying figure 7 reveals two new method sets' having exposed themselves as glaring candidates: they, "stick out," from the bottom of the diagram, the deepest reachable methods. figure 9 shows the first, the positionowningsetnameboxes() method's impacted set.
figure 9: continued probing, a first new candidate.
figure 10 shows the second, that of the stripebackground() method.
figure 10: continued probing, a second new candidate.
though neither impacted set boasts great largess, both suffer from only four incoming dependencies each and so their relocation incurs only minor disruption. figure 11 shows the migration of both impacted sets from sequencediagram . note the depth reduction enjoyed by sequencediagram 's structure as the process unfolds.
figure 11: moving seven more methods.
this second refactoring yields a sequencediagram class as shown in figure 12.
figure 12: sequencediagram after second refactoring.
finally, studying the deeper structures of figure 12 unearths yet another candidate: the impacted set of the processowningsetnamesprinting() method, see figure 13 below.
figure 13: final probe and candidate.
although small, this set of methods extends dependencies onto the newly created class thus motivating its belonging to the new class as much as the donor. figure 14 displays the extracting of these three methods.
figure 14: moving final three methods.
figure 15 shows the final result, the refactored sequencediagram class, a class - taken in isolation - easier to understand with dependencies far easier to trace.
figure 15: class sequencediagram, reloaded.
compare this class to the original, pre-refactored class, see figure 16.
figure 16: the original sequencediagram.
figure 17 shows the final form of the new callingline class, it, too, uncomplicated of structure.
figure 17: the final callingline class.
fear is inexperience.
some programmers dread the task of refactoring large classes because they simply do not know where to begin.
others know better. they know that, no matter how large the class, a suite of simple criteria, iteratively applied, can help illuminate the entire process (a suite, furthermore, that applies to overblown packages, too). these refactorers learn to relish such tasks, seeking out the gigantic monstrosities that lie deep in the heart of most systems, plunging through their event horizons, laughing maniacally at the tickling spaghettification.
photo credit attribution.
Opinions expressed by DZone contributors are their own.