Thank You, Mr. Pearson
Thanks to the Pearson product-moment correlation coefficient, it seems we have a pretty good tool for objectively correlating software properties with costs—giving us a pretty good analytics tool for discovering "bad structure" and helping us to minimize costs.
Join the DZone community and get the full member experience.Join For Free
love of money is the root of all quality.
if a minor functional update to your source code costs pots of cash, then your source code is of low structural quality. period.
hence, the holy grail of commercial source-code structural analysis is finding a software property that objectively correlates with cost, so that programmers can revile this property as, "bad structure," and minimize it to minimize cost.
spot the tricky word in that sentence?
we all have opinions. we all think our software cheap and easy to modify. and, our opinions are not necessarily wrong, but they are mere opinions.
will we ever move beyond opinion? will we ever unearth objective evidence for something as opinion-y as good source-code design?
well, statistics might offer an answer.
you see, statisticians have this almost unbelievable tool called the pearson product-moment correlation coefficient . it crunches two sets of numbers and establishes—mathematically and objectively—whether these sets correlate with one another, that is, whether they tend to change together. for example, you might suspect that programmer salary correlates with experience: the more senior the programmer, the more she gets paid. pearson's coefficient allows you to objectively calculate the degree to which this holds for the entire industry.
but, how can pearson judge software structure?
every method modification entails a cost. some methods are updated more than others and hence cost more. why is this? there are two categories of method update: 1) functionally additive updates which add necessary functionality, and 2) functionally neutral updates which just accommodate structural changes caused by (1).
functionally additive updates take place where they must, indiscriminately, amidst both poorly structured and well-structured code. functionally neutral updates, however—and almost by definition—tend to plague poorly structured code more than well-structured code, because well-structured code offers fewer opportunities for ripple effects to proliferate.
so, we'd ideally like pearson to detect the faint signal from these functionally neutral updates against the great wash of necessary-update background noise.
we do this by making explicit the assumption that, all else being equal, the more a method is updated, the more it costs; and the more it costs, the lower its quality. of course, this does not obtain in all individual cases: some methods may, by chance or design, necessarily require more updates. however, if this bias exists then by examining huge numbers of methods statistically we will eventually expose it.
we then ask mister pearson whether the number of times a method is updated correlates with any of its structural properties.
for example, let's say you suspect that large methods signify poor design and hence the larger the method, the more updates it will undergo. you must then both measure the number of times every method is updated during a series of project releases, and measure the average size of each method during those releases. if your hunch is right, pearson will show you that those methods updated most are also the largest. if your hunch is wrong, then pearson will spank you like a little b... brat in front of all your friends and work colleagues. objectivity has no favorites.
and, we won't stop with method size. let's interrogate all the usual suspects blamed for poor design: number of dependencies, code duplication, the lot. this then will produce no new metric, but will evaluate current thinking.
which methods shall we investigate? to obtain a massive sample size, let's do our usual examination of the update histories of eight open-source projects 1 as tens of thousands of methods churn through successive releases 2 .
finally, pearson's correlation coefficient itself is just a number from -1 to 1.
ignoring negative numbers as they seldom appear in the investigation, a 0 indicates that method size (to continue with the earlier example) has no bearing on the number of times that a method is updated and so offers a poor indicator of development cost. a 1 indicates that the bigger the method, the more it is updated: a perfect development cost predictor.
table 1 below shows the correlation coefficients calculated for all the properties tested over eight programs—labeled p1 to p8—with each coefficient averaged over all 8 programs in the final column.
table 1: pearson correlation coefficients of a method's property vs. number of times that method was updated, over 8 evolving systems.
before analyzing the results, we can quickly double-check our findings by plotting some method properties against the number of times methods were updated for a specific program.
the stronger correlations should show an average trend upwards as the number of updates increases (e.g., the average size of methods updated 4 times should be higher than the average size of methods updated only 3 times), whereas the lower correlations should show no average trend. and indeed this is the case. figures 1 - 4 show four sample properties over all methods of fitnesse during 15 consecutive releases: two properties with strong correlation, two with weak.
figure 1: the size (strong correlation) of all fitnesse methods over 15 releases, and average per number of method changes.
figure 2: the number of dependencies from (strong correlation) all fitnesse methods over 15 releases, and average per number of method changes.
figure 3: the number of dependencies on (weak correlation) all fitnesse methods over 15 releases, and average per number of method changes.
figure 4: the impact set (weak correlation) of all fitnesse methods over 15 releases, and average per number of method changes.
the average column in table 1 presents generally weak correlations between the various properties and the number of times a method changed, but four clearly stand out: method size, the number of dependencies from a method, the number of conditionals in a method, and the impacted set of a method . the more of these, the more updates a method suffers. these, then, are four 4 properties that signify expensive source code.
none of these is a great surprise, of course. everyone knows that we should avoid large methods, cyclomatic complexity, and excessive dependencies. it is, however, nice to see our presumptions' surviving objective scrutiny. even if the correlations are weak, furthermore, they are far stronger objectively than the evidence we often use to justify coding practice to others: personal experience and anecdote.
(it is perhaps more interesting to see which infamous metrics seem not to correlate with cost.)
finally, elephant-in-the-room time.
all together now: correlation is not cause! yes: no one can guarantee that larger methods (for example) cost more to maintain and develop. but, let's face it: most programmers think they do. these correlations do not prove that programmers are right to think this, but suggest that they are not silly for thinking it, either.
Opinions expressed by DZone contributors are their own.