How to Avoid Messy Code
After taking size, impact set, potential coupling, and transitive dependencies into consideration, see how deep your code goes to fend off disorder.
Join the DZone community and get the full member experience.Join For Free
few programmers explicitly intend to write poorly structured source code.
they don't sit down, whip out their bad code design patterns book, and wreak meticulous spaghettipocalypse. rather, poorly structured code is what happens when programmers don't know what they're doing.
figure 1: two java package structures : one well-designed, the other, not so much.
so, why is this difficult?
source code has many properties — and of different kinds.
one property, for instance, is the number of public methods in a program. programmers easily control this property: making a private method public increases the number by 1. and that's it. in a sense, this is a "linear" property, in that small changes produce small effects.
structure also represents a source code property, but calling method b() from method a() does not only affect those two methods. new transitive dependencies form from all methods depending on a() and on all methods depended upon by b() .
furthermore, java has three structural levels: method, class, and package, and method connections need not affect method-level alone. if the owning classes had not been connected, then new transitive dependencies pop into being on class level, too. and similarly on package level. structure thus represents a "non-linear" property, in that small changes may trigger large consequences.
this non-linearity makes writing well-structured programs hard.
it would be helpful if we could forget about this grand, over-arching structure and focus instead on small, linear properties that somehow magically lead to well-structured code.
alas, no such linear properties exist.
but there are hidden clues.
because source code properties are objective, we can measure them. we can certainly count the number of public methods in a program, and hosts of other linear properties besides. we can also measure the "messiness" of a program via the structural disorder , a percentage which rises as source code structure decays. if we measure over a large number of programs, we can then calculate the mathematical correlation between structural disorder and all those other properties.
a negligible correlation would imply no connection between a particular linear property and overall program structure. a large correlation, however, suggests that careful management of that linear property may contribute towards overall well-structured awesomeness.
for example, if structural disorder correlated 100% with the number of public methods, then we might suggest minimizing the numbers of public methods in order to minimize structural disorder, thereby using a simple, linear property to control a difficult non-linear one.
let's give it a whirl.
let's blitz 4 million lines of code from 38 java systems 1 in a code analyzer and get correlatin' over dozens of its structural properties. table 1 shows the strongest structural disorder correlations discovered 2 (full matrices: method , class , and package ).
|average circular dependencies||0.62||0.21||0.58|
|average impact set||0.59||0.35||0.42|
|average impacted set||0.56||0.35||0.42|
|average transitive dependencies||0.56||0.24||0.35|
|average transitive dependency length||0.64||0.24||0.42|
table 1: structural disorder correlations with other properties.
only one property correlates strongly with structural disorder over all three levels: depth .
the depth of a method (class, or package) is just its position in a transitive dependency. in figure 1, on the left, the depth of method a() is 0, the depth of b() is 1, c() is 2, etc. these depths sum to 21. on the right, however, a() still has a depth of 0, but all other methods - being directly called from a() - each have a depth of 1, making the total depth of the right structure just 6.
figure 1: a deep transitive dependency on the left, and shallow dependencies on the right.
it is this depth total that correlates with structural disorder. the deeper your code, the more disordered it'll probably be. if you want to manage your program's structural disorder, avoid deep dependencies 3 .
one way to do this is to use a coordinator ( sunburst ) method, which calls other methods to do the heavy lifting, with the coordinator reduced to a sequencing role. then repeat this pattern on class- and package-level, where possible.
a previous post introduced four evidence-based principles for code structure, where the justifying correlations were weak but non-negligible.
this post adds a fifth principle, "manage depth," but with a much stronger correlation, making the list of evidence-based structural principles now:
- manage s ize.
- manage method i mpact set .
- manage absolute p otential coupling .
- manage the number of t ransitive dependencies .
- manage d epth.
(admittedly, not the catchiest acronym.)
Opinions expressed by DZone contributors are their own.