The Blighttown Corollary
Join the DZone community and get the full member experience.Join For Free
can an image capture an entire system's structural integrity? can we tell from a graphic whether a system is well-structured?
figure 1 shows a spoiklin diagram with method a() calling method b() .
figure 1: two methods.
a trivial system, but is it well-structured? if update-cost indicates structural quality - that is, if the cost of updates rises as structure degrades - and if ripple effect determines update-cost predictability, then figure 1 must indeed be considered syntactically well-structured because of the ease with which potential ripple-effects may be traced. basically, programmers can see how changing any part of this system might potentially change any other. by comparison, figure 2 presents these same two methods but reconfigured.
figure 2: two methods badly structured.
in figure 2, with straight lines showing dependencies down the page and curved lines showing dependencies up the page, the two methods have become mutually dependent: an update to one could lead to an update to the other, and so ad infinitum.
if we ask whether a graphic can capture a system's structural integrity, then for such tiny systems as those of figures 1 and 2, we can answer: yes. but what of larger systems? figure 3 shows a more realistic case, with many more interconnected methods.
figure 3: exhibit a.
examined closely, the figure shows that despite the increased number of methods, structural integrity remains intact.
for example, let us look at the impact set of various methods. the impact set contains all the methods that depend - directly or transitively - on a particular method, and thus reflects the potential ripple-effects of a method's change. figure 4 shows the impact set of the predicate() method (actually a constructor), deep down in the structure where problems - should they exist - most likely fester. though this line of dependencies streaks all the way to the top, it nevertheless remains easily traceable and such traceability, as mentioned, is the very hallmark of good structure.
figure 4: easily traceable impacts.
figure 5 shows the impact set of the select() method. despite having two ripple-effect paths up towards the root method, the paths taken appear again easily traceable.
figure 5: further sunbursts.
now, however, let us examine another system, see figure 6, one comprised of an almost identical numbers of methods as that of figure 3.
figure 6: exhibit b.
take a moment to compare this with the previous system of figure 3 (reproduced below for convenience). which has the better structure? and why?
figure 3: exhibit a.
figure 6 contains some curved lines, dependencies that run up the page. no mere aesthetic, these sometimes represent circular dependencies, a type of dependency which renders the exercise of structural reasoning - precisely what we did with figures 4 and 5, in which we tried to plot the course of hypothetical ripple-effects - far more difficult. figure 6 does not have many curved dependencies - perhaps only 4 - yet because ripple-effects gush through not just dependencies but transitive dependencies, these few dependencies bend the entire system into fearful intricacy.
take the getlinkedfixturewithargs() method highlighted in green in figure 7. lying below the tangle, we might fear a rather complicated path to reach the top of the diagram, and as the diagram shows, these fears seem well-founded.
figure 7: unpredictable paths.
not only might ripple-effects from getlinkedfixturewithargs() traverse the entire tangle, they might pop up at multiple root methods at the top of figure 7. (of course, having multiple client methods represents duplication reduction and not necessarily poor structure; the benefit of duplication reduction offsets but does not neutralize the cost of ripple-effects.)
as another example, consider the green at() method identified in figure 8. true, this lies in the center of the tangle, but we might hope at least that its dependencies filter up the page. the diagram shows the unfortunate extent of its surprising influence.
figure 8: a truly expensive method to change.
different graphical algorithms will display the system differently, but the mutual dependencies of this system, and hence the difficulty with which dependencies may be traced, are properties of the system itself, not of the representing images. nevertheless the hypothesis still holds: graphical representation can identify structural quality, good and bad. but what of even larger systems?
consider the system in figure 9, with almost 150 methods compared to 25 of the previous example (the methods names have been removed to reduce clutter).
figure 9: a system with 150 methods.
is this system well-structured? most programmers would struggle to answer this question on the basis of the diagram alone. yes, we can delve into details, for example examining the impact sets of sample methods, as in figure 10. but our initial question asked whether we can derive an entire system's structural integrity from a single graphic and here any such insight seems buried under sheer weight of method.
figure 10: a specific method's impact set.
perhaps we are examining the system at too low a level? in java, methods reside inside classes, so perhaps we should examine this system on class level rather than at method level to try to ascertain its structure. figure 11 shows the system's class level diagram.
figure 11: the same system at class level.
a far cleaner structure has emerged. in figure 11, every circle represents a class (rather than a method, as in all previous diagrams) and each straight line represents a dependency from class above to class below. now we see that, at class level, the system is well structured, its dominant feature a classic sunburst formation. no circular dependencies or unruly tangles blot the vista. tracing dependencies between classes becomes once more an easy task, as can be seen from analyzing even the deepest class, see figure 12.
figure 12: class-level impact set of the deepest class.
so does good structure on class level imply that the structure on method level in fact was also good if only we could have seen it? what is the relationship between class-level structure and method-level structure? and which scale defines the overall system's structural quality?
how methods map to classes.
the relationship between classes and methods is that of containment. if we consider methods to be the lower level and classes to be the higher, then in mapping methods to classes - we shall call this the, "level-up mapping" - all methods that belong to the same class collapse to a single node on the higher level. similarly, all dependencies between methods within the same class vanish entirely; on class level only dependencies between methods of different classes remain. lastly, a dependency from class a to class b is formed by any number of methods of a that depend on any number of methods of b .
astute programmers will observe that the fundamental nature of a level-up mapping is one of reduction. as methods coagulate within classes, the number of nodes falls. the higher level can never harbour more nodes than the lower nor can it harbour more dependencies.
this allows the derivation of two interesting heuristics. consider the four methods of figure 13, arranged in three transitive dependencies.
figure 13: four methods waiting to be class-ified.
now suppose we group these four methods into two classes, classx and classy , such that a() , b() , c() and d() go into classx and a2() goes into classy , as shown in figure 14.
figure 14: four methods captured in two classes.
how does this system look when viewed up at class level? figure 15 reveals the great un-surprise.
figure 15: class-level view of figure 14.
because all the class-internal methods and dependencies vanish, the system when viewed at class level appears much simpler.
a little thought shows that the number of transitive dependencies of system's higher level is almost always lower than the number of transitive dependencies of its lower level. and the length of these transitive dependencies is also almost always less on the higher level. transitive dependencies being the paths along which ripple-effects do their damage and update costs being the prime indicator of structure, we can conclude that the structure of higher levels is almost always better than the structure of lower levels. structure tends to improve with height.
of course this does not always hold. a programmer reviewing figure 13 might decide to put method b() , instead of method a2() , into classy yielding the alternative method allocation shown in figure 16.
figure 16: an alternative class allocation.
if so then the class-level structure would look as in figure 17.
figure 17: a higher-level circular dependency.
that is, classx.a() calls classy.b() which in turn calls classx.c() , hence creating a circular dependency at class level that does not exist at method level. such cases must always remain a caveat to our analysis - no graphical analysis can be conclusive - though a trawl of real-world systems reveals that few systems contain significant quantities of these beasts (and graphical tools can easily identify them where the turn up). that structure tends to improve with height remains statistically valid.
furthermore, that methods vanish on class level does not remove them from reality. no change of perspective reduces update costs. but when a class bulges with horribly interconnected methods then at least those methods are confined to that class; programmers can bring proven cost-reducing tools (such as information hiding) to bear on them. this goes some way to justifying the structural improvements generally seen on class level.
thus if structure tends to improve with height, then the structural quality of the higher level can be seen as the upper limit of the structural quality of the lower. this leads to the moderate pessimism of the blighttown corollary: whatever the structural quality, it's probably worse underneath .
applying this blighttown corollary to real-world systems takes us the final few steps to the answers we seek.
very large systems.
figure 18 shows the method-level spoiklin diagrams of two real-world systems.
figure 18: method-level diagrams of two real-world systems.
although a surgical analysis of particular methods would undoubtedly prove useful, these figures do not give the programmer an overall impression of structural quality. both systems built from thousands upon thousand of methods, the diagrams contain simply too much information.
this presents a clear instance of a graphical representation's not being powerful enough to convey useful structural information.
so might it help then to go up a level? figure 19 shows both systems again, but on class level, where each circle represents a class (or interface).
figure 19: class-level diagrams of two real-world systems.
once again, the amount of information in both diagrams renders impossible an overall evaluation of either system's structural quality. it simply is not obvious whether these systems are susceptible to ripple-effects and costly updates. both systems may be well-structured and cheap to update, but this information is lost amid graphical noise.
perhaps going up yet another level might help? figure 20 shows both systems again, but on package level, with each circle representing a package and each line a dependency between packages.
figure 20: package-level diagrams of two real-world systems.
suddenly, useful structure crystallizes from chaos.
now the programmer can clearly see that tracing the dependencies running through the system on the left remains difficult: this is a poorly designed package structure. the system on the right, however, allows easy tracing of dependencies and potential ripple-effects: with all lines running straight, it is clear, for example, than any changes to the line of packages towards the top (by far the largest proportion of the system) have a low probability of impacting the rest of the system. this is a good package structure.
but does a good package structure imply a good system structure? we must assume that by, "system structure," we mean structure on all levels: method, class and package. yet for large systems, graphical representation can only suggest overall structural quality on package level, the lower levels offering sensory overload. has scale finally defeated interpretation? well, the blighttown corollary applies not just to method and class levels but to all levels. crucially, it allows us to relate levels to one another.
the blighttown corollary says that given the good package structure on the right, the class and method structures underneath are probably worse, but this really tells us little. it is not especially useful to know that something is worse than good (though knowing that good structure exists on package level informs us that a good system structure is at least possible).
given the poor package-structure on the left, however, coupled with the blighttown corollary's predicting that class and method level structures are probably worse, we can say that such an image bodes ill for this system. if the package structure is bad and the lower levels are worse, then they must be bad indeed. this system's entire structure must be suspect.
so, can an image capture an entire system's structural integrity? well, no, not conclusively. but the image can place probabilistic constraints on the structure's quality; and if that image contains the highest-level structures of the system, then the image can certainly suggest an expected degree of quality for the whole system.
the blighttown corollary highlights the importance of a good package structure, as this structure will probably constrain the quality of the entire system's structure. little can save the method and class levels when the package level degrades. indeed, a java software shop without a clear strategy for managing package dependencies may find updates puzzlingly expensive no matter how many refactoring cycles they endure. sometimes the roof determines the foundations.
if you want a well-structured system, look up.
Opinions expressed by DZone contributors are their own.