Is Your Code Structured Like This?
Join the DZone community and get the full member experience.Join For Free
junit's evolving structure.
junit is a masterpiece.
as martin fowler tells us , "junit was born on a flight from zurich to the 1997 oopsla in atlanta. kent (beck) was flying with erich gamma, and what else were two geeks to do on a long flight but program? the first version of junit was built there, pair programmed, and done test first."
it is rather difficult to think of any other piece of java software that has made a greater contribution to java software quality.
kent beck has done an equally noble service to our field by releasing the junit source code as it was written, thereby bequeathing us a permanent historical overview of its development.
let us avail of this gesture. let us take a rather superficial journey through junit's evolution by studying, not the enormous value it has brought or the lines of code which realised that value, but its package-structure .
we shall look at each release's package-structure as a spoiklin diagram in which a circle will represent a package, straight lines will represent dependencies from packages drawn above to those below and curved lines will represent dependencies from packages drawn below to those above. the colour of a package will indicate the relative number dependency tuples (that is, transitive dependencies) in which it partakes: the more red, the more dependency tuples.
we shall examine those releases most readily-available , from versions 3.7 up to 4.11.
are we sitting comfortably? then we'll begin.
the early phase.
figure 1: junit version 3.7.
figure 1 shows version 3.7 and perhaps one aspect seems most striking.
the diagram is lovely.
we can see at a glance, for example, that
depends only on
. a confusable lot, programmers value such clarity, drawing from it a warm sense of no-overtimeness.
we should not, of course, read too much into a package-diagram. we do not know, for example, whether the
package holds five hundred or five million lines of code. deeper investigation will show that no monsters lie hidden under beds here but we shall confine ourselves mostly to structure for the moment.
structure being a set of elements and their dependencies, most would conclude that junit version 3.7 is a well-structured work. let us move on to version 3.8
figure 2: junit version 3.8.
in version 3.8 the
package has packed its bags but otherwise the structure is unchanged. if anything it appears even more aesthetically-pleasing than before; some might consider figure 2 a thing of beauty. uglier constructions certainly besmirch half the tee-shirts that amble by you daily.
so far so good.
figure 3: junit version 4.0 (version 3.9 was not in the repository).
something has happened in version 4.0.
the diagram is still relatively clean-looking despite the number of packages having almost doubled from six to eleven. a nagging doubt has surfaced, however. in some qualitative way the elegance of version 3.8 has, if not been lost, at least suffered some tarnishing.
curved lines have appeared indicating our first dependencies going up the page. in itself, this is insignificant; spoiklin's algorithm produces such curves as artifacts rather than damning moral judgments.
a curved line only raises concern where it forms the infamous, "bow," pattern of mutually-dependent elements and such, alas, is the case here. mutually-dependent packages invite suspicion. mutually-dependent packages suggest exposure to one another's ripple-effect changes and this troubles programmers.
figure 3 reveals three such bows:
still, the package-structure looks manageable and concerns are allayed with release 4.1 whose structure is identical with that of version 4.0. let us move on to version 4.2.
figure 4: junit version 4.2.
version 4.2 is also similar to 4.0; a single new package is added,
. the bows that arrived with version 4.0 have failed to metastasize, structural degradation appears arrested.
version 4.3, however, is growling around the corner.
the middle phase.
figure 5: junit version 4.3.
version 4.3 represents by far the largest structural change in the history studied here. beneath the surface, the number of functions leaps from 564 in version 4.2 to 1309 in version 4.4 (it will fall almost as dramatically in the next release), though the number of packages rises from eleven to just sixteen. (this non-commensurate rise in the number of packages may help explain the fall in configuration efficiency from 31% to just 18%.)
the introduction of the
packages seems the most significant change yet the authors have performed this introduction quite successfully. a good test of a structure is the ease with which one can point to any element and identify the other elements it depends on directly and transitively. though there are many,
's dependencies are readily traceable.
also, we note that the number of bows remains unchanged: the surge of functions has not triggered an increase in mutually-dependent packages.
figure 6: junit version 4.4.
figure 6 shows version 4.4 and
leaves us before we really got to know it. the number of functions falls from 1309 to 853; the number of packages rises to 19.
among the new packages to make their entrance are the
cluster (bottom right) which also unfortunately bring three more mutually-dependent packages to the party. still, this cluster shies from the main group. we may have some difficulty counting the number of bows in that main group (has it increased by 1?) but a diligent programmer could still hope to identify all transitive dependencies from a randomly chosen target.
this is perhaps the last time that such is the case.
the late phase.
figure 7: junit version 4.5.
a difficulty in tracing transitive dependencies characterizes the late phase of junit's package-structure.
figure 7 shows version 4.5. 150 functions more than version 4.4, the system boasts a configuration efficiency risen from 29% to 34%. its twenty-six packages, however, have become embroiled.
for the first time, a programmer introduced to this system might pause before accepting the assignment. no doubt the drawing-algorithm generating the diagram must take some blame: a better algorithm might do a better job. no algorithm, however, can veil the brash rise in inter-connectedness on display.
, previously to one side, has gravitated towards the big players, tugged by new dependencies from both
. it will remain center-stage until its sudden elimination in version 4.11.
how many mutually-dependent packages do we have? who can say? if we change a package, can we easily predict which clients will be affected? can say with any certainty which clients will not be affected?
such questions dog all later revisions so let us skip forward to the last: all intermediates are visible in the animated graphic at the top.
figure 8: junit version 4.11.
by version 4.11, transitive dependencies have proliferated seemingly unchecked. we are far from the short dependency-chains and few cyclic-dependencies of good structure.
and this ultimately is the point.
a software's user-experience realization is just another term for what that software does. this user-experience realization and its structure are, however, practically orthogonal . perhaps hundreds of thousands of programmers use junit daily. it is a phenomenally well-tested piece of software and it works beautifully. yet this tells us nothing about its structure.
structure is not about execution: it's about development cost. the greater the inter-connectedness of a structure, the more ripple-effects can occur during updates and the more costly those updates can grow.
you cannot test good structure into a design. junit exemplifies this: it was written test-first but its structure has nevertheless degraded significantly with time.
testing - at least unit- and acceptance-testing as we generally know them - is about user-experience realization. we write unit tests such as, "verify that bank account balance does not fall below zero." we do not write unit tests such as, "verify that package a has no dependencies on package b, c, d or z." yet the latter is precisely the type of consideration we must make when we create structure.
there is another way.
it doesn't have to be like this.
we can build package-structures that scale well with program size.
there are many ways to do this, but one way is to practice radial encapsulation . figure 9 shows the evolution of a radially-encapsulated program that is far bigger than junit yet has throughout its history retained a structural clarity that junit seems to have abandoned.
figure 9: a radially-encapsulated program (end configuration efficiency=69%).
programmers should be forced to wear their systems' package-structures on their tee-shirts.
practice radial encapsulation.
junit is a masterpiece.
Opinions expressed by DZone contributors are their own.