The Spectacular Instability of Good Code Structure
A previous post noted that source code structure decays because there are many more ways to build a poorly structured system than a well-structured one, and so, without strict structural guidelines, a system will inevitably explore deeper into disorder state-space until it finds the dark nirvana of ball-of-mud-ness from which few escape. This short post merely demonstrates the phenomenon graphically.
Join the DZone community and get the full member experience.Join For Free
a previous post noted that source code structure decays because there are many more ways to build a poorly structured system than a well-structured one, and so, without strict structural guidelines, a system will inevitably explore deeper into disorder state-space until it finds the dark nirvana of ball-of-mud-ness from which few escape. this short post merely demonstrates the phenomenon graphically.
figure 1 shows a spoiklin diagram of a well-structured system.
figure 1: a well-structured package diagram.
in figure 1, each circle is a package, each straight line a dependency from a package above to one below, each curved line (there are none yet) from a package below to one above.
a system is well-structured when its package dependencies are easy-to-trace, when it readily suggests how updates to any package may ripple to others. the dark red packages at the bottom of figure 1 are heavily-depended upon, and hence may be expensive to modify; the packages along the top are relatively isolated and thus may be cheap to modify.
now, let's say the team responsible for this system hires a terrible programmer—a mister ropy, "do call me 'ent'!"—one who disregards the structural guidelines evidently in place. this chap knows better. he does his own thing. to impress management, ent initiates a batch of 10 "refactorings": he moves ten classes to packages in which he thinks they more correctly "belong".
the result of this first batch of refactorings is shown in figure 2, side-by-side with the original for comparison.
figure 2: the original and a first batch of 10 refactorings.
what is your immediate reaction to this? can you see the difference between the system in this before-and-after-refactoring figure? and, which system do you think is more well-structured? specifically, which system's dependencies are more easily traced?
the team quickly reviews this batch of "refactorings" and votes to refuse permission to commit to trunk. not to be dismissed, however, our new young go-getter comes up with another batch of 10 refactorings—this batch even more correct than the previous. figure 3 shows the original system side-by-side with the results of this second batch .
figure 3: the original and an alternate second batch of 10 refactorings.
again: which system do you think is better? which system's dependencies are more easily traced?
the team quickly deletes the branch again. mr ropy, astounded, spends all weekend perfecting a final best ever batch of 10 refactorings, once more moving just 10 classes between packages. figure 4 shows the results.
figure 4: the original and an alternate third batch of 10 refactorings.
ent is fired.
he is fired because his "refactorings" were nothing of the sort. refactorings improve system structure: they make dependencies easier to trace, not harder. ent's refactorings simply sucked.
figure 5: the package diagram of fitnesse.
no sooner is he in the door that he embarks on a brave new batch of 10 refactorings, a batch that will make the product just so much easier to work with and leave his manager gob-smacked. the refactorings again simply migrate 10 classes to different packages, and figure 6 shows the result, alongside the original system.
figure 6: the original and a first batch of 10 refactorings.
immediate reaction time: has this batch of refactorings made the system better or worse? are dependencies more easily traced before the refactorings or after?
the product team declines the kind offer and deletes the branch. ent, predictably, ruminates and devises batch number 2, see figure 7.
figure 7: the original and an alternate second batch of 10 refactorings.
and this time, he's thought ahead. he's prepared an alternatve third batch of refactorings simultaneously, for extra bedazzlement. see figure 8.
figure 8: the original and an alternate third batch of 10 refactorings.
what do you think of mister ropy's efforts on this second system?
few programmers could easily tell whether any of these batches made the system's structure better or worse. you could probably guess (the third seems worse, perhaps?) but it is not immediately obvious.
now, the shocking reveal: mister ropy does not exist! the six batches above were chosen entirely randomly: the class to be moved was randomly selected, as was the target package.
of course, no one would actually make changes like this, but the point here is that your system should be so well-structured that the degradation brought about by batches of random refactorings should be immediately, screamingly obvious to all. in this sense, good structure is fragile and unstable, in that even small changes can produce large harmful effect. poor structure, on the other hand, is remarkably stable: you can make a great series of changes to it, but its properties will remain more or less the same. order tends to disorder, and disorder tends to stay just where it is, thank you very much.
if you were developing that first system above and arrived in on monday morning to see any of those "refactored" package diagrams, you'd mash the emergency wtf-alarm and lock-down the entire building to hunt for the culprit.
if you worked on the second system, honestly, would you even know anything had changed when you started your week's work?
to put it another way: if you can make random structural changes to your system without noticing a clear degradation, then your system's structure is random. it is maximally disordered. your ability to predict modification costs will be minimized, and the actual costs will bear little relation to the size of the modification you wish to effect.
the question is: would you want a mister ropy to join your organization? are you confident enough in your system's structure to believe that his incompetence would be exposed? and, would you know if he were already working beside you?
the universe hates programmers.
when it comes to behaviour, we want our programs to be stable: we want them to react uniformly and predictably under widely varying conditions. but, all programs want to do—as every programmers knows—is throw nullpointerexceptions as far and as fast as their digital arms allow.
when it comes to structure, we want our source code organization to be as unstable as possible so that we can clearly see how modifications to one part might affect others. but, all source code structure wants to do—as every programmer knows—is collapse at the speed of light into the most widely deployed source-code structure on earth: the ball-of-mud.
and, when it comes to refactoring, life's a batch.
Published at DZone with permission of Edmund Kirwan, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.