Metrics Considered Charmful
Metrics are great! Or are they? Here's a delightful look at metrics, and how charmful they can be.
Join the DZone community and get the full member experience.Join For Free
testing can be abused so we should stop writing junit tests. the legal system can be abused so we should close the courts. government power can be abused to we should ditch democracy.
now just wait a gosh-darned...!
these are, of course, ludicrous arguments: that a process can be abused provides no justification for its eradication.
yet something of this mindset hovers over the programming community's view of source code metrics. most programmers view code metrics reasonably, seeing them as an extra pot of information about - though hardly the definitive appraisal of - the state of their code. some, however, when faced with metrics, cite goodhart's law , hint that the mere choosing of a metric to measure will automatically invalidate it, and proceed to build code without formulating metrics at all.
this is a shame.
the great benefit of metrics is that they shunt subjectivity off minds and onto fingertips, transporting discussion from the realm of ideas to the crisp black-and-white of source code. you may or may not agree on the representative value of a particular source code property, but while that property remains a thought exercise, all parties are free to imagine and argue for (or against) their own varying interpretation.
once you convert that property into a metric - and, better still, into a algorithm running in a source code analyzer - you've made the property objective, common to all views and truly negotiable.
take, for example, package coupling.
oh, we all loooooooooooooooove moaning about sara's hideously coupled packages and micheal's ridiculously high-carb spaghetti-fests, but what do we really mean? how do we measure package coupling?
(disclosure: this blog is no great fan of the terms, "coupling," and, "cohesion," due to their inherent contradictions , and what is described here should rather be called, "sub-level dependencies," but the term, "package coupling," used in isolation, seems harmless for now).
let's take a well-known java open source project at random: apache maven . figure 1 shows a spoiklin diagram of the packages in maven's core jar file, with each circle representing a package, each line a dependency between two packages.
figure 1: maven core package structure.
a beauty queen this is not, but is package structure the same as package coupling?
if you ask the average programmer in the corridor, she might say that package coupling represents the "strength" (in some sense) of dependencies between packages, and that this this "strength" increases as more classes/interfaces are involved. so if package a depends on only one class in package b , then a is weakly coupled to b , but if a depends on ten classes in b then a is strongly coupled to b . so let's redraw figure 1 but with thickness of line relative to the number of classes depended upon, see figure 2.
figure 2: maven core package coupling.
good, we're getting somewhere. immediately we can see that coupling reveals what structure alone does not, namely that a few central packages are heavily dependent on one another. and we can put a figure on our coupling: maven core has 393 class dependencies that span package boundaries, so its package coupling, according to our provisional definition, is 393.
and the fun doesn't stop there. let's take this baby out for a spin: let's explore some implications of our definition. figure 3 (a work commissioned in state-of-the-art microsoft paint) shows a system of just two packages. package a has two classes - x1 and x2 - where x2 depends on three classes in package b .
figure 3: strong package coupling of three dependencies.
so the package coupling of the system is 3. but could we refactor this system to reduce package coupling (without just lumping everything into one package)? well, we could move class x2 into package b , as shown in figure 4.
figure 4: refactoring to weaken package coupling?
by doing this, we have reduced the system's package coupling to just 1. of course, other factors influence good design: we do not blithely shove classes around just to minimize coupling. nevertheless, it's interesting to see what might result from such blind minimization.
and now, with our metric, we can write a simple algorithm to move a class sequentially into all packages of the system, record the move the yielded the lowest coupling, migrate the class permanently to that package and repeat the process for all other classes until no lower coupling can be found.
doing so on maven produces 100 coupling-reducing refactorings, each of which you can see applied in figure 5, in all its twitchy splendor (doesn't it look oddly ... organic? like time-lapsed footage of neuron samples growing on that petri-dish gel that microbiologists are so fond of?).
figure 5: 100 refactorings to minimize maven's package coupling.
figure 6 shows the resulting, refactored maven.
figure 6: maven with package coupling minimized.
maven's final package coupling is 168, a 54% reduction. is this an improvement? perhaps, perhaps not. it certainly looks less tangled, but our naive algorithm seems to have evacuated entirely those packages along the top, and beneath the surface, that internal package has bloated to almost three-times its initial size. this was, however, a first attempt: we could easily tweak the algorithm to, for example, prevent increasing the size of the largest package, prevent moving classes on which multiple other classes depend, prevent emptying a package entirely, etc.
and note that this attempt has not collapsed the system into a single package: every connected package remaining in figure 6 has resisted the onslaughts of these barbarian refactorings. why have these survived? why have others been emptied of classes? why have so many come to depend on internal ? even this first attempt has raised interesting design questions.
the point here is that by clearly defining what we mean by, "package coupling," we have created a metric that allows objective system evaluation from that perspective, and that allows us to probe our own understanding of what package coupling means.
finally, this was just one choice of design property: there are countless other properties we all think good design should have. why don't we have well-established metrics for them all?
there's nothing wrong with design principles that cannot be metricized (just look at popularity of the single responsibility principle - could you write an algorithm to detect, "responsibility," in source code?).
but that a design principle does not admit a metric is a concern. code reviews can always descend into those ape-like screaming sessions when we have wishy-washy principles to club one another over the head with.
metrics are not the solution: no metric will ever capture semantic constraints, for example. metrics are, however, just one more way clarify what on earth we're all shouting about. and when has more clarity ever been a bad thing?
in your face, goodhart!
Published at DZone with permission of Edmund Kirwan, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.