I'm a programmer. Or at least, I intend to be a programmer but it's taking an awfully long time. I'm starting to wonder whether being a programmer is more about the journey towards being a programmer than being a programmer. Edmund is a DZone MVB and is not an employee of DZone and has posted 34 posts at DZone. You can read more from them at their website. View Full User Profile

The Structure of Spring (Core)

05.03.2013
| 4459 views |
  • submit to reddit

 

Spring core evolution

OMG!

Look, whatever you're doing: stop.

If you're at work, go home.

If you're at home, grab a beer from the fridge.

If you have dog, grab a beer for him, too.

Banish the kids to their rooms.

Send your husband out to get his hair done or your wife out to mend the car.

Send your neighbours to the cinema.

Because the girls and boys from Spring are about to give you the greatest package-structure masterclass of your entire sorry-assed life.

Note the time, because you'll want to look back at the moment when all your Java projects' package-structures were suddenly rendered ugly, embarrassing and obsolete.

You are about to become a better person.

Striking gold.

What had become a gloomy search for structural excellence among the the most popular, open-source, Java programs slams into Spring, the, " ... most popular application development framework for enterprise Java." Spring being too large to study as a single entity, this analysis will concentrate on the jar-file of the core components. (And thanks to @etiennestuder for suggesting Spring as a candidate for analysis.)

As usual, we shall look at each release's package-structure as a spoiklin diagram in which a circle will represent a package, straight lines will represent dependencies from packages drawn above to those below and curved lines will represent dependencies from packages drawn below to those above. The colour of a package will indicate the relative number package-dependency tuples in which it partakes: the more red, the more dependency tuples.

Seriously, hang on to something.

Beginneth the enchantment.

Spring core version 0.9

Figure 1: Spring core version 0.9.

It all starts so well but if this series has taught us anything it is that most applications start well. Figure 1 shows us the nine, well-structured packages of Spring's core jar-file housing around eight hundred functions. Pretty as a picture, it almost invites update so that we can effortlessly tip-toe along the ripple-effect paths.

So maybe a mutual dependency twangs between util and beans but what's a single mutual dependency between friends?

Spring core version 1.0

Figure 2: Spring core version 1.0.

Version 1.0 contains more-or-less the same number of functions as 0.9 but the number of packages increases from nine to eleven with that big, bad access barging into dominance, spraying dependencies far and wide.

Still, stark order remains; even the util/beans mutual dependency has vanished.

Spring core version 1.1

Figure 3: Spring core version 1.1.

Figure 3 sees the number of functions rise to over a thousand and a third support package making an entrance, all gracefully absorbed by that resilient structure. io has developed a dependency on util and propertyeditors is making eyes at io, to note a few of the changes, but nothing untoward disturbs the peace.

Excellence of structure has now survived three significant upgrades; "Not a bad record for this vicinity." Fingers crossed that all goes well for whatever next mass influx of functionality that the Spring engineers have in store.

Spring core version 1.2

Figure 4: Spring core version 1.2.

Eh?

The number of functions shrivels to 369. Some sort of Year-Zero-Event, then. That code review must have been the mother of all bloodbaths.

Not really much to say about this.

Spring core version 2.0

Figure 5: Spring core version 2.0.

Version 2.0 returns to business as usual with almost nine hundred functions dolloped into ten packages and producing a structure which looks even better than that of the first version, 0.9.

The achingly-pretty simplicity finds itself reflected in two key statistics.

Firstly, the structure is so wonderfully shallow with an average function-dependency tuple length of just 3.0 (std. dev. 1.6). Depth ruins good structure, bulldozing all before it into the landfill of complication. Indeed, depth had been a minor concern in version 1.1 which had an average function-dependency tuple length of almost ten - considered slightly high - but The Great Contraction of version 1.2 seems to corrected this trajectory: average dependency tuple length will never again rise above an impressively-low six.

Secondly, as a direct consequence of the shallowness the number of function-dependency tuples composing the core components hovers at just over one thousand. This figure will become a concern again - we'll see that later - but to have the number of tuples so close to the number of functions strikes as an extraordinary feat of software engineering, producing one of the least-coupled designs this series has witnessed.

Spring core version 2.5

Figure 7: Spring core version 2.5.

Version 2.5 maintains its structural composure even in the face of an extra nine packages and six hundred functions.

Tracing potential ripple-effect paths remains a breeze. Package-spotters - up with the dawn mists, woolly-hatted, thermos-flasked - scribble to record the excited arrival of the asm package: it will disappear immediately into those dawn mists and pop back up in version 3.2.0. where it will play a prominent role in the tuples-problem.

Spring core version 3.0.0

Figure 8: Spring core version 3.0.0.

Version 3.0.0 accretes another three hundred functions, bringing the total to eighteen hundred, and sheds two packages, resulting in an almost-perfect crystalline structure. That such a vision represents a real-world, working-for-a-living, get-your-hands-dirty system rather than an ideal toy created in a structure tutorial belies belief.

Beneath the surface, yet another key statistic enjoys improvement. The core components' configuration efficiency has to date languished at around twenty per-cent but this version sees it leap to thirty-four per-cent and it will rise slowly but steadily with each new upgrade.

Spring core version 3.0.5

Figure 9: Spring core version 3.0.5.

Version 3.0.5 brings with it an extra two packages and one hundred and fifty functions. The result? Continued, flawless structure.

Spring core version 3.1.0

Figure 10: Spring core version 3.1.0.

Version 3.1.0. adds yet another three hundred functions, bringing the total to almost two thousand three hundred.

An ever-so slight blemish has appeared in those curved lines but these are mostly dismissible as drawing artifacts rather than indicators of deeper degradation. That the system maintains its integrity cannot be doubted.

Spring core version 3.2.0

Figure 11: Spring core version 3.2.0 with 4521 functions.

Version 3.2.0 - last stop on our tour - represents by far the largest upgrade in the Spring core components' history displayed here: the number of functions virtually doubles to four thousand five hundred while the number of packages soars to thirty-two.

Yet how does this massive increase in functionality impact the structure? Have the invading hoards brought the system to its knees?

Not at all.

Figure 11 radiates confidence and control and complete structural mastery.

The Spring team has stood firm before the onslaught, battling the potentially-overwhelming functionality by simply expanding their structure to embrace it. The upshot is a configuration of packages that oozes decoupling and clarity-of-dependency. The aim of good structure is to aid update-cost prediction and figure 11 does precisely that.

Facing such a diagram we all too easily lose sight of just what an accomplishment it represents. The secret of success lies in making the difficult look easy and in this the Spring programmers have alas excelled themselves. Without immediate comparison some might think that figure 11 is somehow normal in the field of software engineering, or even common.

This would be a mistake.

Compare figure 11 to the Ant package-structure, for example, when it grew to a similar number of functions:

Ant version 1.6.0

Figure 12: Ant at 4603 functions.

The Struts core components barely rose above three thousand functions yet recall that package-structure:

Struts version 2.3.8

Figure 13: Struts core components at 3022 functions.

And JUnit never rose above thirteen hundred functions:

JUnit version 4.11

Figure 14: JUnit at 1295 functions.

Spring makes these look like crayon scrawls on padded walls.

Not all roses.

But even Spring has its problems.

Though the package-structure thrived in the massive upgrade to version 3.2.0, a problem arose lower down: the number of function-level dependency tuples leaped from below four thousand in 3.1.0 to twenty-six thousand in 3.2.0. At the same time the number of function-level circular dependencies shot from below three hundred to above five thousand.

That rise in circular dependencies suggests that the proliferation of dependency tuples stems from some amplification effect; it would be a shame not to peek just a little closer to try to find evidence for such a claim.

And evidence there is. For when we look again we find that the function through which most dependency tuples flow is ClassReader.accept() in the asm package, a function whose cravat of immediate dependencies is shown in figure 15 (in which circles represent functions rather than packages).

ClassReader.accept()

Figure 15: ClassReader.accept()

From the naming scheme the experienced programmer immediately recognizes this as a visitor pattern which may go a long way towards explaining the problem: for such a dense concentration of dependencies offers a perfect nesting-ground for amplification effects.

Furthermore, the asm package, in which this function resides, is a sink: it has no dependencies on any other packages in the core components.

Both points suggest that the exorbitant number of dependency tuples - which might otherwise spoil the value of so fine a package structure - may be due not to systemic dependency disarray but a mere amplification-effect, one mostly confined to a single package.

On the whole, then, we can applaud this magnificent work for breath-taking structural achievement that it truly is. Spring takes the top leader-board position and by an absurdly-long way.

Summary.

Thank you, Spring.

Just thank you.

Published at DZone with permission of Edmund Kirwan, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)