Java Legacy Hairball: Refactoring Case Study
Ten years ago, I led a team for a banking client to rejuvenate their FX-trading platform. One aspect of that mission was to snip away at the existing singleton-laden solution in order to make a cleaner and more unit-testable. Check out this slide deck from back then used to convince the client to start the rejuvenation work.
Join the DZone community and get the full member experience.Join For Free
ten years ago, i led a team for a banking client to rejuvenate their fx-trading platform. one aspect of that mission was to snip away at the existing singleton-laden solution in order to make a cleaner and more unit-testable. i’ve written the case-study for this methodical (and pausable ) decomposition project over a few prior articles:
- march 20th, 2013 » a singleton escape plan
- march 11th, 2013 » legacy app rejuvenation
- april 01st, 2008 » drinking your guice too quickly?
- april 26th, 2007 » introducing branch by abstraction
from the 2008 article—the one that introduced most depended-on and least depending component as a strategy—an attempt to find a visual metaphor for entanglement/disentanglement:
today though, a slide deck from back then used to convince the client to start the rejuvenation work.
note: for the sake of this article ‘singleton’ is the gang of four pattern , not the spring/guice idiom.
eleven slides showing the first refactoring
architecture as described by the client:
the starting point was the logical business tier:
it turns out, it was not as decomposed as hoped for:
make room in the diagram for more boxes:
introduce service locator and a place for refactored components:
identify the **most depended on, and least depending**, component:
move it to new codebase (make sure its unit tests come too and rejuvenate those):
in old codebase, two depending components now look-up the new component via the service locator. part new and part old, the app/service should go live at this point:
identify another component to move (most depended-on and least depending):
move it as before. this time with a pre-existing dependency (via service locator):
in the old codebase, add one more looks-up to the new component (via the service locator). repeat the last three steps until there are no singletons left:
a methodical nature to this
the refactoring effort could pause at any stage (as there are higher priorities for the dev team), or carry on methodically with other business priorities. indeed, the app/service going live at a certain cadence perhaps should be part new functionality and part refactoring. to get there, you really want to be doing trunk based development, and perhaps even continuous delivery. if this work is being done on a separate branch, with the promise of a merge back later (and a go live), there is an increasing risk of the business halting the initiative, perhaps irrevocably with career-impacting consequences.
the project back in 2006?
we were concurrently migrating from multi-branch clearcase to single trunk perforce. components were being shaped up first in clearcase, and removed as they were being put in the perforce trunk in a hierarchical source tree and build (maven style, but in ant). things from clearcase that needed components now in perforce found them in ‘binary repo’ (webdav then, but like sonatype’s nexus or artifactory). gradually, components (and their tests) migrated out of clearcase and into the perforce trunk with a hierarchical build. cruisecontrol (a precursor to jenkins, etc.) had a build that would create the binary artifacts in the repo, as well as use them in builds. that ci pipeline would be triggered by commits to one of two scms and well as binaries being replaced in the binary repo.
all the while, teams that were not focussing an iteration on the migration would carry on with business functionality. the sum of the business functionality and the refactoring was put live in the same release, on an unchanged (or bettered) cadence. the business never knew the refactoring was going on, or that test coverage was increasing.
the guiding principal was to attack the “ most depended-on and least depending component ” only. each time that was achieved, there’d be a new candidate for that in the hairball.
a couple of years later, at google, i worked with angular-creator misko hevery’s intern (david rubel) on the google singleton detector . this helps easily visually identify most depended-on and least depending component for future initiatives. helped, perhaps, it might not work with java 8/9 anymore.
after all the singletons are gone, what next?
go to dependency injection, of course. you would do this in a second series of refactorings and stop when there nothing using the service locator any more. the latter has to go still, as it facilitates undesirable static mutable state.
danilo sato shows you how with intellij idea, in this super impressive video:
ref: refactoring experiment on vimeo.
danilo does one refactoring at a time pushing service-locator lookups up to the constructor, then into the constructor’s parameters, and finally towards the startup class that composes the solution.
danilo’s blog entry that accompanies the video: refactoring strategies: a walkthrough experiment .
Published at DZone with permission of Paul Hammant, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.