The 80% Rule of Program Coupling
The 80% Rule of Program Coupling
How understanding the fundamentals of software structure is a great way to earn job security.
Join the DZone community and get the full member experience.Join For Free
look, you want job security in the software industry? here's how you do it.
examine software fundamentals, tease out the problems associated with those fundamentals and learn to solve those problems. these fundamentals are shared by all computer languages, across all applications, are unchanged in forty years and are unlikely to change any time soon. as are their associated problems. hence your workplace desirability.
the most fundamental of these fundamentals reveals itself in a trivial observation: software is composed of interacting parts. it has structure.
the timeless problem of structure is that when you change one part, you may also have to change others because these interacting parts... interact. this means locking horns with your favorites: coupling and ripple effects . from a programmer's — and corporate accountant's — point of view, if a program's heavily coupled, its design is crap. no buts. no ifs. no arguments. if a program is not heavily coupled, then it may or may not be crap; only further investigation will tell. low coupling does not imply good design, but presents a crisp, inescapable prerequisite.
so, how do you tell — at a glance — whether a program's heavily coupled? you measure a property of its methods. then you establish a threshold coverage — say, 80% 1 — such that only when 80% of the program's methods meet a certain property value can that program be considered loosely coupled. if it doesn't, then that puppy's heavily coupled and crappage ensues.
so what property do you measure? how do you measure coupling 2 ?
take method x() and count all the methods that depend on it either directly or indirectly: that's your property. that's the impact set of x() , the most likely set of the methods which (in a worst case scenario) might have to change when x() changes. that simply is the coupling.
combining both ideas, your job-securing, lightning-fast program evaluation will look like this: 80% of this program's methods are depended upon by up to 10 other methods: this program's design is crap. but pay me and i'll do much better!
or: 80% of this program's methods are depended upon by up to 17 other methods: ugh. you really need me working here.
or: 80% of this program's methods are depended upon by up to 23 other methods: my rate's doubling with every minute.
but... which figure do you choose? 10, 17 or 23? is there an absolute figure for sloppy design? well, no. but this litmus test allows us to compare any two programs. so let's turn the problem around and re-examine some java programs that this blog's already studied (and others besides) to find a nice low coupling figure to aspire to. once we identify the good, all the rest will be the bad.
figure 1 shows the impact set for spring 3.2.0.rc1 3 .
figure 1: spring 's impact set.
yeah, so it's a cdf graph and graphs put you in the kitchen at parties. but this one's interesting, honest: find 80% on the y-axis, follow it across till it hits the curve and read down to the x-axis. thus: 80% of spring's methods are depended upon by up to 8 other methods . is this good? is this high or low coupling? we shall see.
figure 2: the impact set of junit 4.11.
80% of junit's methods are depended upon by up to 9 other methods . we're going in the wrong direction. we want fewer depending methods, not more.
figure 3: the impact set of lucene 5.2.1.
80% of lucene's methods are depended upon by up to 9 other methods . no help here.
figure 4: the impact set of log4j 5.2.1.
80% of log4j's methods are depended upon by up to 6 other methods . wow, that seems pretty good. its 80th percentile impact set is 33% lower than, for example, lucene's; you might say it's a substantial 33% less coupled. well done, log4j. but can we go lower?
figure 5: the impact set of ant 6.2.1.
80% of ant's methods are depended upon by up to 8 other methods . we're heading in the wrong direction again.
figure 6: the impact set of fitnesse 20151230.
80% of fitnesse's methods are depended upon by up to 10 other methods . eeeewww ...
figure 7: the impact set of antlr 2.7.7.
80% of antlr's methods are depended upon by up to 12 other methods . omfg!
then we come ...
... to netty ...
figure 8: the impact set of netty 4.0.36.
just look at that jaw-dropping low coupling. 80% of netty's methods are depended upon by up to just 5 other methods!
here, then, is the gold standard. and netty's no tiny, easy-to-design toy: it has 13,000 methods of loosely coupled gorgeousness. so if netty's programmers can do it, why can't you?
and netty's not even alone. here's jgroups.
figure 9: jgroups ' impact set.
and this little minx.
figure 10: the impact set of spoiklin soice .
the conclusion may be drawn that building systems according to the 80% rule - with an 80th percentile impact set of just 5 methods or fewer - is a demonstrably achievable goal, and because it's achievable, larger 80th percentile values suddenly become commercially questionable.
so there's your job security: learn of how to design programs as good as netty and your pension's safe.
yes, this post glossed over a slew of subtleties. the 80% threshold is arbitrary. some programs might conceivably be less coupled even higher thresholds. everything described here is a metric and all metrics can be faked 4 . and many more besides.
despite this, the metric above would seem to capture the age-old phenomenon of coupling.
do not think for a moment that coupling has been solved and modern programmers have simply wafted on to sexier challenges. fundamental problems do not "age" away. as you can see from the above examples, coupling continues to generate massive and unnecessary costs in our industry.
yet clearly some people can manage it better than others. the programmers of netty look forward to comfortable retirements. do you?
(thanks to mairbek for pointing out netty as a design worthy of study.)
Opinions expressed by DZone contributors are their own.