Is Your Code Too Concrete?
Edmund Kirwan looks at the number of connections and dependencies, and how they signal a transition to the infamous big ball of mud pattern.
Join the DZone community and get the full member experience.Join For Free
"take 20 buttons and a ball of string, and scatter the buttons over the floor. then take two buttons at random, and connect them using a thread. do this for a while ... pick up a random button, and count the number of buttons it is attached to."
apparently, a threshold number of connections emerges below which picking up a button lifts few others with it, but above which a large number of buttons will be snagged (the referenced paper shows that this, "giant component," will contain half the buttons).
considering software, mark then wonders whether such a threshold of dependencies might signal a transition to the infamous big ball of mud pattern and investigates with some clojure experiments.
this post will tread a similar path, but will analyze the effect on two types of dependency. the experiment above focuses on direct dependencies, dependencies between just two methods (strictly speaking, "binary dependencies," may be a more accurate term, but the connection to compiled code may confuse).
good structure, however, concerns the reducing of the impact costs of ripple effects , and ripple effects do not limit themselves to direct dependencies but instead race along transitive dependencies, which are direct dependencies laid end to end. figure 1 below, for example, contains three direct dependencies - a() b() , b() c() and b() d() - but contains only two transitive dependencies - a() b() c() and a() b() d() .
figure 1: three direct dependencies, two transitive dependencies.
so we shall explore the relationship between direct dependencies and the transitive dependencies they spawn.
let us generate dozens of systems of 1000 java methods randomly connected by increasing amounts of direct dependencies, then count the number of transitive dependencies that result. is there a "transition point" at which the number of transitive dependencies suddenly takes off, reflecting a horribly interconnected and expensive-to-maintain structure? see figure 2.
figure 2: systems of random direct dependencies.
the red crosses in figure 2 show (the blue line is the theoretical trend) that as long the number of random direct dependencies remains well below the number of methods (1000), then the total number of transitive dependencies stays relatively low, indicating systems that would be cheap to maintain.
as the number of direct dependencies approaches the number of methods, however, then the number of transitive dependencies does indeed soar from around 800 to 10,000. when, furthermore, the number of direct dependencies rises to just 10% more than the number of methods, then the structure completely collapses with nightmare scenarios of 70,000 transitive dependencies and more.
so, can we expect real systems with more direct dependencies than methods to also show this catastrophic structural degradation? table 1 shows the relevant data for some recently reviewed systems.
|methods||direct dependencies||transitive dependencies||abstract|
table 1: analyzed programs.
table 1 shows that almost all these programs have more direct dependencies than methods, yet only ant exhibits the expected disastrous structure with half a million transitive dependencies. why do the others show so few transitive dependencies?
it turns out that the model above is a little too simplistic to be applied to source code: it suffers from two flaws.
firstly, it takes no account of java interfaces. an interface method is abstract; having no body it cannot call others methods, and so cannot be the source of a direct dependency.
secondly, it presumes direct dependencies to be uniformly distributed, but real programs demonstrate a power-law of dependency distributions, with some methods being the target of vastly more direct dependencies than others.
in well-structured systems, furthermore, both these points reinforce one another. with gof's , "program to an interface, not an implementation," and martin's , "depend on abstractions not on concretions," large percentages of direct dependencies terminate on abstract methods. table 1 shows the actual percentage figures for those analyzed programs in the right-most column (several ironically low), with ant's figure hidden till later as a test of a theory.
let us run the 1000-method experiment again, but this time let us force 16% (casually chosen from table 1) of all direct dependencies to terminate on abstract methods, see figure 3.
figure 3: random direct dependencies with 16% terminating on abstract methods.
comparing figures 2 and 3, we see that in figure 2, the number of transitive dependencies rose to 10,000 (on average) with just 10% more direct dependencies than methods. in figure 3, with 16% abstract-terminating dependencies, the system rises to 10,000 transitive dependencies at around 20% more direct dependencies than methods.
whether we choose 10,000 to represent structural chaos or some other number, the principle of, "program to an interface not an implementation," seems to delay the onset of that chaos.
a figure of 16% abstract-terminating dependencies indeed seems quite low. how does the chaos-threshold move as we add more and more interfaces? see figure 4.
figure 4: random direct dependencies with 16%, 25%, 35% and 50% terminating on abstract methods.
figure 4 suggests the trend continues, with a system of 50% abstract-terminating dependencies remaining well structured with vastly more direct dependencies than less well-abstracted systems.
of course, all of these analyses lack the scope and statistical rigour to form definitive conclusion. they suggest, however, the crucial importance of abstraction in the fight against structural complexity.
as a final finger-in-the-air test, this theory would predict that ant's embarrassing showing in table 1 is due to its having a very low number of direct dependencies terminating on abstract methods.
its actual figure?
program to an interface not an implementation.
depend on abstractions not on concretions.
eat your vegetables.
these principles exist for a reason.
Opinions expressed by DZone contributors are their own.