The Golden Age of Spaghetti Code
According to Edmund Kirwan, the greatest trick spaghetti code ever pulled was convincing the world that it didn't exist.
Join the DZone community and get the full member experience.Join For Free
which of following two java package structures is the least well-designed?
oops! let's try that again.
figure 2: two java package structures, junit and spoiklin soice.
this blog's banged on and on about how much the package structure on the right in figure 2 (in which circles are packages, straight lines represent down-the-page dependencies, and curved lines are up-the-page) is better than that on the left because the right's structure presents clearer dependencies, making update costs easier to predict and to update themselves often easier to implement.
two problems, however, persist.
firstly, graphical evaluation is subjective. yes, most would agree that the structure on the right is better, but consider figure 3.
figure 3: two more messy java package structures, lucene and struts.
which do you think the messier structure in figure 3? (the answer is in table 2.)
secondly, diagrams such as these offer insight when we evaluate a small number of nodes, as on package-level, but fail before the ghastly node-pocalypse of class- and method-level.
figure 4: a java method-level structure. good luck with that.
what we need is to objectively quantify spaghettiness : its messiness, its disorder. how on earth do we do that? what makes a structure messy?
fortunately, mathematics has already defined what spaghettiness is by defining its opposite: total order . and we can apply this to computer programs, with just one teensy supposition.
mathematics says that if a set has a binary relation with just three specific properties, then that set enjoys total ordering. let's go through it.
consider the three methods in figure 5, where method a() calls b() and b() calls c() , forming the single transitive dependency: abc .
figure 5: three simple methods.
from this diagram, we must extract a set of numbers. we'll extract our old friend depth, where depth is method's position in the transitive dependency. thus, a() is at position 0 (because programmers), b() is at position 1, and c() is at 2.
figure 6: three simple methods numbered by their depth in a transitive dependency.
mathematics tells us that this "program" is totally ordered with respect to depth, if, when you extract these depth values and iterate over them in pairs – a pair of depth values being, say, d1 and d2 – then the following properties hold:
if d1 >= d2 , and d2 >= d1 , then d1 == d2 . (duh.)
if d1 d2, and d2 d3, then d1 d3. we'll come back to this.it is always true that either d1 d2 or d2 d1.
the first and third properties are rather trivial, but that second property says that if we write out our transitive dependencies then depth values should never decrease. and in figure 6, they do not: a(0)b(1)c(2) .
as our program's depth satisfies all these properties, then it is totally ordered. we have achieved mathematical objectivity.
figure 7 shows a slightly more complicated program of two transitive dependencies, again with methods' depths indicated.
figure 7: oooooh! two transitive dependencies.
both transitive dependencies separately satisfy the three properties required.
now let's look at a bad boy. suppose someone grabs this code and calls e() from c() , that is, creating a dependency from c() back up to e() .
figure 8: our first messiness.
recall that curved lines represent dependencies that go up-the-page and with c() now depending on e() we have the transitive dependency: a(0)b(1)c(2)e(1)f(2) , in which the depth value decreases at one node . this transitive dependency is therefore not totally ordered, so we cry carbohydrate!
thus we can now define our metric. no, not "spaghettiness." let us channel our inner squares and call it "structural disorder." a transitive dependency is structurally disordered if it does not satisfy the total order properties above, and a program's overall structural disorder is then the percentage of disordered transitive dependencies.
let's take this puppy out for a spin.
looking at the two packages structures in figure 2 once again, we would intuitively expect the structure the right to be far less disordered than that on the left, and it turns out to be so:
figure 9: junit disorder is 76%, but spoiklin soice disorder is 3%.
although we seek an objective measure, we nevertheless expect that as structures become subjectively messier-looking, their structural disorder values should rise. we can test this by taking two perfectly structured systems, "refactoring" them by applying random dependencies between nodes and checking whether their disorder values generally rise as their structures collapse. see figure 10.
figure 10: two sad, decaying systems.
we can even simplify matters by defining the (admittedly arbitrary) categorization whereby a program suffering from 50% disorder or more is spaghetti. the threshold might have been 40% or 60% - feel free to choose your own. in fact, we'll have four categories, distinguished by garish, child-friendly color coding: red and black=naughty, green and white=nice.
table 1: the four categories of structural disorder.
let's point our disorder-binoculars at 15 java programs, some quite well-known. table 2 shows the programs and their structural disorder percentages on method-, class- and package-level. you'd expect most professionally designed programs to be "good" to "fair" on the disorder spectrum, so the table should appear overwhelmingly green and white, yes?
table 2 : the structural disorder percentages of 15 java programs.
it seems that we, as professional programmers, can write more or less well-structured methods, but above that...rrrr mrrrr grrrrd.
three points are noteworthy.
first, we chest-bump endlessly about refactoring, yet refactoring definitionally involves just one thing: improving software structure. table 2 suggests that we fail to consider refactoring at class- and package-level.
second, higher-level structure can provide a model, a simplified view, of the lower levels: a good package structure, for example, can offer a great map of functionality without pushing the programmer's nose into foul code. yet our higher-level models seem vastly more disordered than that which they model. table 2 suggests that we fail to maximize the benefits of higher-level structure.
thirdly, oracle will release java9 any day now (honest!) with its new modules , offering a level of structure above even package-level . yet we apparently lack the desire or competence to manage the levels we already have. table 2 predicts the rise of spaghetti modules.
figure 11: not another code review... (source here ).
so, are we still writing spaghetti code?
hell, yes! not only are we still writing spaghetti code, we're living in the golden age of spaghetti code, an age in which we professional programmers don't just observe and casually ignore spaghetti, we don't even recognize it in the first place.
the goto statement used to be the alarm that forced programmers to manage control flow in their programs. abandoning the goto statement, however, in no way removes this concern but rather migrates control flow to the realm of inter-method, inter-class and inter-package dependency where (in those last two cases, at least) its complexity now thrives, far from the programmer's gaze.
the greatest trick spaghetti code ever pulled was convincing the world that it didn't exist.
this is my structure. there are many like it, but this one is mine.
my structure my best friend. it is my life. i must master it as i must master my life.
without me, my structure is useless. without my structure, i am useless. i must design my structure true. i must design cleaner than my enemy who is trying to out-structure me. i must embarrass him before he embarrasses me. i will...
my structure and i know that what counts in programming is not the variables we rename, the methods we extract, nor the conditionals we replace with polymorphism. we know that it is reduced disorder that counts. we will reduce disorder...
Opinions expressed by DZone contributors are their own.