Software Engineering as... Engineering
Learn how engineers and software developers think differently through cumulative distribution functions (CDFs).
Join the DZone community and get the full member experience.Join For Free
how do you compare the structure of two different programs?
you could fire up your ide and dive in. a week's snorkeling will no doubt produce a valid conclusion. if, however, you have only ten seconds to decide then you need some other tool.
engineers facing similar problems in other fields regularly look to the cumulative distribution function (cdf) for results and software engineers could learn much from them.
firstly, though, the usual (for this blog) preamble: well-structured programs facilitate update predictability. one reason why updates deviate horribly from prediction is ripple effect, whereby changing code in one place causes changes in another, which lead to further changes, etc. two factors greatly influence ripple effect: transitive dependency number and length. hence programs with more and longer transitive dependencies than others tend, all else being equal, to be less well-structured.
so our task becomes: how do we compare the number and length of transitive dependencies in two different programs? (we shall investigate here dependencies between methods, as opposed to classes or packages.)
merely counting transitive dependencies yields a poor comparative tool: larger programs tend to have more transitive dependencies. instead, we should compare the transitive dependency density: the number of transitive dependencies divided by the number of methods, thus normalizing the quantity. program a's having a higher density than program b suggests program a's structural inferiority.
to compare transitive dependency lengths, however, we can use the cdf (actually a variant thereof) of the transitive dependency length. for example, figure 1 presents the cdf of junit 's transitive dependency lengths.
figure 1: the absolute lengths of junit's transitive dependencies.
the x-axis of figure 1 shows the lengths of junit's transitive dependencies: all its transitive dependencies are between 1 and 20 methods long. the y-axis shows the percentage of junit's transitive dependencies that are equal to or less than a particular length.
the graph reveals that 50% of junit's transitive dependencies are around 5 methods long or less. 90% are around 11 methods long or less. and as junit's longest transitive dependency is 20 methods long, 100% of its transitive dependencies must be less than or equal to 20 methods long.
but this does not yet allow a straight comparison as the absolute values of the x-axis would mean that two programs might occupy different ranges, skewing the graph. instead of absolute transitive dependency length, however, we can plot on the x-axis the percentage of the maximum length than any specific transitive dependency has, see figure 2.
figure 2: the lengths of junit's transitive dependencies as a percentage of the longest.
it may look odd to have percentages on both axes, but figure 2 shows that 60% of junit's transitive dependencies are approximately 30% (or less) as long as the longest transitive dependency. and 90% of junit's transitive dependencies are around 55% (or less) as long as the longest transitive dependency.
well-structured systems waft this curve to the left: with more short transitive dependencies than poorly-structured systems, they boast higher percentages of their transitive dependencies being lower percentages of the maximum length.
finally, we can now plot multiple systems side-by-side, see figure 3.
figure 3: a multi-system comparison.
the numbers in the legend of figure 3 show each system's transitive dependency densities.
well-structured systems veer upwards quickly, so comparing the six systems in the 30% - 50% percentage length is revealing. an unsettling gap yawns between extremities: only 25% of ant 's transitive dependencies are less than 30% of maximum, whereas 70% of spoiklin's transitive dependencies are less than 30% of maximum. coupled with ant's eye-watering 22 transitive dependencies per method, this strongly suggests ant's less-than-ideal structure.
spring offers a beautiful structure on package-level but, as has been mentioned before, on method-level it suffers greatly from long transitive dependencies, with 30% of its transitive dependencies stretching longer than half of the maximum length.
junit , fitnesse and struts all trace similar trajectories, with 80% of their transitive dependencies being less than half of the maximum length. so similar are their curves that their densities alone may decide their structural fates, with junit and struts both showing good, ripple-effect-bustingly low densities.
a cdf offers an overview of an entire system, at a glance and from a particular perspective, making it highly suitable for the analysis of large complex software systems. of course, cdf graphs alone do not categorically evaluate structural magnificence or decrepitude; extreme outliers can compromise results; some snorkeling remains advised. cdfs can, however, offer invaluable and speedy insight.
ps if you know of any open source java you'd like to see structurally analyzed, please suggest it in the comments below.
Opinions expressed by DZone contributors are their own.