Exploration Through Example

Example-driven development, Agile testing, context-driven testing, Agile programming, Ruby, and other things of interest to Brian Marick
191.8 167.2 186.2 183.6 184.0 183.2 184.6

Wed, 11 May 2005

Approaches to legacy code

Some recent and pending encounters with legacy code have made me think there may be three different broad approaches.

Rewrite and throw away
In this approach, you declare that the application will be broken for some period of time. Like a cartoon character, you dive into it, produce a furious cloud of dust that obscures you (picture gears and such flying out of the cloud), and finish with a new thing (or a thing with a shiny new component installed).

Refactor into submission
This is the approach that Feathers teaches in his fine Working Effectively With Legacy Code. In it, you gradually wrap pieces of the application in tests, gingerly fixing dependencies to make that tractable. Then, when some part of the code is wrapped and decoupled, it can be changed safely. I think of this as islands of order growing amongst the seething chaos of the product.

Strangle
The term strangler application is due to Martin Fowler. The image is of a vine growing up around a tree, gradually killing it, until eventually the only thing left alive is the vine, roughly in the shape of the original tree. You don't (much) fix up existing code. Instead, when you need something new or changed, you begin by building fresh, greenfield code. The legacy code can call into the greenfield code, but access in the other direction is minimized and highly controlled. (I think it's fair to say a project Strangling its application is using Feathers's Chia Pet Pattern (pdf, slide 9) as the overwhelmingly dominant tool/metaphor.)

As the last parenthetical remark suggests, these approaches shade into one another. Nevertheless, they seem to me three distinguishable stances toward legacy code. My question is: how do you decide which stance to take?

In the 60's, renowned software engineer Rocket J. Squirrel drew on his extensive experience to make the definitive comment on Rewrite and Throw Away: "That trick never works!". Which isn't invariably true, of course, but it's a risky strategy. The other two methods can still deliver a steady stream of features to the business, albeit at a slower rate (in the short term). In this one, the team vanishes from view of the business, doing who knows what, promising great things for the future. That's an unstable situation, because the business is liable to get fed up and start insisting on features ASAP. It's also a dangerous situation for the programmers. Because they have to finish everything before anything works, they might not know they're way off course until far too late.

Still, Rewrite and Throw Away is easier than refactoring into submission. When faced by a tangled mass of code, it's hard to know where to start. And, in my (lack of) experience, you're much more likely to have a refactoring that flames out and has to be rolled back, just because of some complex interconnectedness you didn't grasp at first. Until you've learned-through-doing, it's frustrating, and frustration leads to short-cuts (which is how you got the legacy code in the first place, probably).

Strangling the application has the advantage that you've consciously decided you shan't fix the old code, so the dangers of touching it are (somewhat) lessened. It also has the advantage that you can quickly create a concrete architecture (a set of layered subsystems, say) to guide your forward movement. (This insight is due to Michael Thomas, who's writing me an article on strangling code.) I don't have the experience to speak to Strangling's disadvantages (except that I can imagine long debates about the concrete architecture preventing people from getting moving).

I have this idea that there must be a set of patterns or lore that would help people navigate among those choices. Who will write it down?

## Posted at 09:14 in category /coding [permalink] [top]

About Brian Marick
I consult mainly on Agile software development, with a special focus on how testing fits in.

Contact me here: marick@exampler.com.

 

Syndication

 

Agile Testing Directions
Introduction
Tests and examples
Technology-facing programmer support
Business-facing team support
Business-facing product critiques
Technology-facing product critiques
Testers on agile projects
Postscript

Permalink to this list

 

Working your way out of the automated GUI testing tarpit
  1. Three ways of writing the same test
  2. A test should deduce its setup path
  3. Convert the suite one failure at a time
  4. You should be able to get to any page in one step
  5. Extract fast tests about single pages
  6. Link checking without clicking on links
  7. Workflow tests remain GUI tests
Permalink to this list

 

Design-Driven Test-Driven Design
Creating a test
Making it (barely) run
Views and presenters appear
Hooking up the real GUI

 

Popular Articles
A roadmap for testing on an agile project: When consulting on testing in Agile projects, I like to call this plan "what I'm biased toward."

Tacit knowledge: Experts often have no theory of their work. They simply perform skillfully.

Process and personality: Every article on methodology implicitly begins "Let's talk about me."

 

Related Weblogs

Wayne Allen
James Bach
Laurent Bossavit
William Caputo
Mike Clark
Rachel Davies
Esther Derby
Michael Feathers
Developer Testing
Chad Fowler
Martin Fowler
Alan Francis
Elisabeth Hendrickson
Grig Gheorghiu
Andy Hunt
Ben Hyde
Ron Jeffries
Jonathan Kohl
Dave Liebreich
Jeff Patton
Bret Pettichord
Hiring Johanna Rothman
Managing Johanna Rothman
Kevin Rutherford
Christian Sepulveda
James Shore
Jeff Sutherland
Pragmatic Dave Thomas
Glenn Vanderburg
Greg Vaughn
Eugene Wallingford
Jim Weirich

 

Where to Find Me


Software Practice Advancement

 

Archives
All of 2006
All of 2005
All of 2004
All of 2003

 

Join!

Agile Alliance Logo