The Ripple Hypothesis
Let's delve into the math of ripple effects, their causes, their effects, their importance when coding, and how to avoid them in your code.
Join the DZone community and get the full member experience.
Join For Freeperhaps my blog's most quoted piece, a single paragraph from a 1974 paper forms the basis for essentially all software structure:
"the fewer and simpler the connections between modules, the easier it is to understand each module without reference to other modules. minimizing connections between modules also minimizes the paths along which changes and errors can propagate into other parts of the system, thus eliminating disastrous, 'ripple effects,' where changes in one part cause errors in another, necessitating additional changes elsewhere, giving rise to new errors, etc."
the authors understand the changes to propagate from modules dependedon to depending modules, as they later write, "for example, only two modules ... might make use of data element x in an 'included' common environment ... yet changing the length of x impacts every module making any use of the common environment."
but is this true?
do ripple effects exist ^{ 1 } ? are they supported by empirical evidence, or do we just take them on faith? and if they exist, do they really propagate from dependedon to depending modules?
let's get down'n'geeky.
let m be any method of program s .
let c _{ m } be the set of methods that m directly depends on, the children of m .
let a _{ m } be the set of methods that directly depend on m , the antecedents of m .
figure 1: with dependencies drawn downwards, a _{ m } = {a1, a2, a3}, the antecedents of m, while c _{ m } = {c1, c2, c3, c4}, the children.
let's define function δ() as identifying change, where a "change" is a difference in compiled bytecode between two consecutive revisions. so δ( m ) is a change in method m between two revisions. we can then define the conditional probability of the change in m given a change in any of its children over all consecutive revisions, namely:
p(δ( m )  δ( c _{ m } ))
similarly, we can define the probability of a change in m given a change in any of its antecedents:
p(δ( m )  δ( a _{ m } ))
then the ratio of the two defines the ripple bias of m , r _{ m } :
p(δ( m )  δ( c _{ m } ))  
r _{ m } = 

p(δ( m )  δ( a _{ m } )) 
we can stretch this to the ripple bias of the entire system by averaging probabilities over all methods in s :
p(δ( m )  δ( c _{ m } ))  
r _{ s } = 

∀ m ∈ s 
p(δ( m )  δ( a _{ m } )) 
if ripple effects bubble up from children then this ratio will be greater than 1; if they tumble down from antecedents, it will be less than 1. if nothing but a programmer's pizzafueled imagination gave fevered birth to ripple effects one lonely winter's night, this ratio will be almost exactly 1.
so let's make the bold claim that the average ripple bias for a large collection of n programs is greater than 1, that is, for k=1..n :
r _{ s k } > 1
this is the ripple hypothesis . it claims that ripple effects exist and they leap from dependedon to depending methods. the question, however, remains: is it true?
fortunately, this mathematical treatment allows us to measure ^{ 2 } approximate probabilities using real java code, if sufficiently large numbers of measurements are made on sufficiently large numbers of changes. thus, 64 million lines of java were analyzed as 20 programs ground through 22 thousand revisions (details here ). the measured ripple bias suggests that the hypothesis is indeed true:
r _{ s k } = 1.5
that is, ripple effects exist and are 50% more likely to ripple up than down ^{ 4 , 5 , 6 } .
this fundamental asymmetry at the heart of software structure exerts profound influence. the ripple hypothesis makes two further measurable predictions.
firstly, given a changed method, the number of changes it undergoes should correlate more with its number of children than with its number of antecedents. (a correlation is a number between 1 and +1, where +1 suggests a strong connection between two sets of data and 0 suggests no connection whatsoever.)
measuring the same codebase above confirms this: the average changed method has a correlation of 0.28 with its number of children, but just 0.01 (essentially negligible) with the number of its antecedents.
secondly, if children primarily trigger ripple effects then it follows that a method with no children — a method that depends on no other, an "independent" method — should suffer fewer changes than a method that does depend on others — a "dependent" method.
it was found, measuring the codebase again, that this is the case: dependent methods, per line of code, are a whopping 92% more likely to change than independent methods.
most programmers will find all this tediously obvious, so why bother with this analysis?
two reasons.
firstly, it offers a perspective shift.
figure 2, presented many times before, shows a poor structure on the left and the improved version on the right with reduced depth , refactored according to the siptd principles .
figure 2: a deep transitive dependency on the left, and shallow "sunburst" dependencies on the right.
the ripple hypothesis allows us a slightly different interpretation. instead of seeing the figure on the right as a depthreduction, we can appreciate it as a maximization of the number of independent methods: the figure on the left has 1 independent method, the figure on the right boasts 6. and as independent methods suffer fewer ripple effects, the righthand configuration should be cheaper to change.
secondly, it offers objective evidence to defend programmers in their choice of code structure.
when a code reviewer asks why you structured your code in a certain way, it's fine to say, "because of the single responsibility principle," but be prepared to argue what responsibility you mean, how many others there are, and whether you've prioritized the list as would the reviewer. if, however, you're asked why you have one method calling many, you can always say, "because an analysis of 64 million lines of code showed that independent methods cost less."
(mic drop.)
this investigation, furthermore, focused on methods, but there is no reason to assume that ripple effects fail to haunt class and packagelevels as well. so if the ripple hypothesis is sound then, to paraphrase spock , we would be wise to accept its conclusion. namely, pepper your code with sunbursts at all levels. the finance department will thank you*.
(the observant among you will have spotted a slightly disturbing implication of all this, an implication to be explored in the next post.)
*the finance department won't thank you. they don't thank anyone. beancounters!
Opinions expressed by DZone contributors are their own.
Comments