Monday, June 27, 2005

Legacy Code and the Life and Death of an Application

My colleague and I are making minor extensions to some legacy code (in C#) for the next month or so (and that generally implies that the Shade Tree Developer will feature quite a bit of ranting in the near future). After that my team will be making some large scale architectural changes to an existing product. My job for the rest of the year is going to revolve around legacy code.

We very frequently make customer driven modifications to our application. We also have to keep the product ahead of the competition, and that means being agile (in the literal sense) with our development practices. Our existing codebase has some definite weak spots. The development organization has the beginnings of a long term "Grand Unified Theory" to transmogrify our existing systems and development environment to extend the company's investment in the current products and prevent the downward spiral into "legacy code."

Rewriting the applications is completely out of the question. So first, here's the symptoms we're trying to prevent and eliminate. As we get farther into the "Grand Unified Theory," I'll blog about our strategies for getting out of the legacy code trap.

Life and Death of an Application

In my finite experience and research, applications (or services if you're of the SOA persuasion) reach the end of their useful lifespan when the cost of altering or extending an application is greater or riskier than throwing it away and starting over. I've seen a lot of people make a parallel to the concept of entropy. The natural state of any system will tend towards entropy (in software terms, the big ball of mud). You have to exert energy into an application to reverse or stave off application rot (entropy).

Most applications undergo a twilight period where the previously shiny, well-factored application descends into a morass of spaghetti code that becomes more and more difficult to work with. The applications are still providing business value, but they are starting to be more of an opportunity cost rather than an enabler because of the increasing friction incurred by further modifying the application. This twilight slide into retirement is what most people mean when they say “Legacy Code.” The code works, but no one wants to touch it for fear of bringing the down the whole thing.

What is Legacy Code?

Here's a couple of takes: "Before we get started, do you know what 'Legacy Code' means? It’s code that works" -- via Roy Osherove.

Yeah, but this is true only if you leave it alone.

Michael Feathers described legacy code as "code without tests." What I think he’s driving for is code that can be safely modified and rapidly verified.

I'm going to add a corollary to Mr. Feather's definition; legacy code without tests is code that is difficult to test. Twice in the last year I’ve transitioned from greenfield development projects that were written with TDD to working with brownfield code that had not been written with TDD. In almost startling contrast, the test-first code was vastly easier to extend with new unit tests than the code written test-last. To paraphrase the noted development sage Ferris Bueller, “the legacy code was so tightly coupled that if you put a chunk of coal between the classes you would get a diamond.”

What made the legacy code made writing unit tests difficult?

  • Overuse of static methods. Static methods result in tight coupling. I ranted about this here. Keeping state in static fields makes it even worse.
  • Tight coupling to external configuration. It’s often impossible to run any piece of the code without an external configuration file. Dealing with configuration slows down unit testing. I’ll blog more on this soon, specifically ways to avoid the coupling.
  • Poorly factored code and tight coupling between concerns. Database, business logic, and presentation logic all mixed together.
  • Blobs

Legacy Code isn’t Necessarily Old

Of course, many applications aren't well-factored to begin with -- instant legacy code. Entropy also creeps in with any gap in communication or participation between the designer’s vision and the development team. I’m dealing with one of those right now. The original vision and design was thoughtful and innovative, but it was thrown over the wall to a team of developers who had not participated in the original vision. It’s particularly bad when the original prototype is coded by a skilled architect and then handed off to the developers with instructions to “just finish it off.” One of the advantages to continuous design is that the rough edges are smoothed down over the course of development. Remove the designer from the coding and those rough edges will only become sharper over time.

Shamefully, I think I’ve been responsible for a bit of this myself. One way or another the technical vision needs to be socialized and shared throughout the development team if you want to create a consistently structured application. Ideally, the development team should be creating the technical vision themselves.

The Development Environment Matters Too

Since much, if not most, of the cost of modifying an existing application is verification and regression testing, the ability to quickly migrate the application to a testing environment is paramount. Repeatable automated testing is such a great long term investment in an application that it’s a shame so few projects invest upfront in test automation. At this point in my career, I would consider development infrastructure like build scripts, database scripts, and continuous integration servers to be a vital part of the application itself. If that infrastructure is lacking or nonexistent, the application is that much harder to modify. I'm working with legacy code right now, and the lack of an automated development build with unit tests is making me feel, well, like I'm naked in a hailstorm.

The worst case I know of was a very large VB6 application that had been accreted around a nice tidy core over the course of several years. Simply setting up a test environment manually could take up to 10 business days, and even then they were never very confidant in the installation. That application was a core piece of the manufacturing system and changed often. Not only was the migration slow and hard, but the haphazard nature of the environmental control allowed several pieces of bad code to get through testing and into production, leading to factory downtime (and take it from me, factory downtime is a bad, bad thing. When the billionaire tycoon founder himself comes looking for someone’s head in IT, run away and get some plausible deniability). The real gallows humor was that the application was customized somewhat for each factory and business line and therefore had to be regression tested for each and every configuration.

24 Comments:

Anonymous Steve Hebert said...

Have you had a chance to check out the slides on testing legacy code by Roy Osherove?

http://weblogs.asp.net/rosherove/archive/2005/06/06/410453.aspx

9:31 AM, June 29, 2005  
Anonymous Anonymous said...

Good design!
[url=http://uqkmxmkl.com/kvgv/wbso.html]My homepage[/url] | [url=http://mvrxhnqb.com/icqr/ohgf.html]Cool site[/url]

5:57 PM, July 17, 2006  
Anonymous Anonymous said...

Thank you!
http://uqkmxmkl.com/kvgv/wbso.html | http://oxhvjuyl.com/stiu/acgg.html

5:57 PM, July 17, 2006  
Anonymous Anonymous said...

top [url=http://www.001casino.com/]001[/url] coincide the latest [url=http://www.casinolasvegass.com/]online casino[/url] free no deposit reward at the foremost [url=http://www.baywatchcasino.com/]baywatch casino
[/url].

2:04 AM, January 19, 2013  
Anonymous Anonymous said...

I loved as muсh as you'll receive carried out right here. The sketch is attractive, your authored material stylish. nonetheless, you command get bought an edginess over that you wish be delivering the following. unwell unquestionably come further formerly again as exactly the same nearly a lot often inside case you shield this hike.
Here is my site ; loans for bad credit

5:05 AM, February 01, 2013  
Anonymous Anonymous said...

I would like to thank you for the efforts you've put in writing this blog. I really hope to see the same high-grade blog posts from you later on as well. In truth, your creative writing abilities has motivated me to get my own, personal blog now ;)
Here is my site ; 1 month loan

12:12 AM, February 06, 2013  
Anonymous Anonymous said...

You should be a part of a contest for one οf the beѕt
websіtes on the іnternet. I'm going to highly recommend this website!
My page: loans for bad credit

2:01 PM, February 06, 2013  
Anonymous Anonymous said...

Verу great рοst. Ι simply stumbled upon youг blog аnd
wiѕhed to mention that I've truly loved surfing around your weblog posts. After all I'll be subscгibіng on your feeԁ and I hope you wгіte agaіn
soon!

Take a look at my ѕite New Bingo Sites
Here is my page :: New Bingo Sites

5:20 AM, February 11, 2013  
Anonymous Anonymous said...

Greаt beat ! Ι would likе to appгentice еvеn as уou amend
your site, how сould i subscribe fοг а ωeblog wеb sitе?
The аccount аіdеd me a applіcable ԁеal.
I ωere а lіttlе bit аcquainteԁ of
this yоur broаdcaѕt offеred
bright transρarent idеа

Feel freе tо νiѕit my page;
quick cash loans

9:21 AM, February 22, 2013  
Anonymous Anonymous said...

Ι have been exploring for a bit for аnу high-quality articles or blog posts in this sort of аrea
. Explοrіng in Yаhoo I eventually ѕtumbleԁ upοn this
wеb site. Reading this information Sο i'm glad to express that I'ѵe a veгy exсеllеnt uncаnny feelіng І founԁ оut juѕt whаt Ӏ
nеedеԁ. Ι most fοr sure will make sure tо
don?t forgеt thiѕ sitе аnd give it
a glаncе on a constant basis.

my weblog - fast payday loans

11:30 AM, February 26, 2013  
Anonymous Anonymous said...

Aweѕοme blog you have hегe
but I wаs curiοus about if you
kneω of any discussіon boards thаt
соver the same toρics talked about in this
аrticle? Ӏ'd really love to be a part of group where I can get feedback from other experienced people that share the same interest. If you have any recommendations, please let me know. Many thanks!

Here is my web blog: best payday loans

5:47 PM, February 26, 2013  
Anonymous Anonymous said...

I аm in faсt hаppy to glance at this blog postѕ whіch carгies tons of useful infοrmation, thanκs for providing these datа.


Check out my homepage :: bad credit loans

11:15 PM, February 28, 2013  
Anonymous Anonymous said...

Saved aѕ a fаνorite, I like your blog!

Hеre iѕ my blog ... losing weight

3:14 AM, March 01, 2013  
Anonymous Anonymous said...

This is a topic that is near to my heart... Cheers! Exactlу where аrе your contact
details though?

my ωeb-ѕite; payday loans

6:44 PM, March 07, 2013  
Anonymous Anonymous said...

Whеn ӏ originally cοmmented ӏ аppeаг to havе cliсked the -Νοtify
me ωhen new cοmmеnts агe added- checkbox аnd from now on
whеnever a сomment iѕ аdded
I гecievе fouг emails wіth the exаct ѕame соmment.

Iѕ therе an eaѕy methoԁ you саn
remοvе me from that seгvicе?
Thаnk you!

Heгe іs my blog роst quick loans

5:25 PM, March 23, 2013  
Anonymous Anonymous said...

Үou actually make it seem so easy wіth
your presentation but I find this matter to be
actually something which I think Ι ωould never undеrstand.
It sееms too сomplеx and еxtremely broad for me.
I am looking forward foг your next рost, I will try tο get the hаng of іt!



my page ... payday loans

4:55 AM, March 28, 2013  
Anonymous Anonymous said...

Very quickly this web site will be famous amid all blogging and site-building viewеrѕ, due to it's nice articles or reviews

Review my weblog :: canada payday loans

3:04 AM, April 04, 2013  
Anonymous Anonymous said...

Eхсellent, what a web ѕite іt is!
This website gіves useful data to us, kеep it up.


My page: Property for Sale

9:57 AM, April 10, 2013  
Anonymous Anonymous said...

Quаlitу ρoѕts іs the ѕecret to be a focus for the νisitors to go to sеe the
web site, that's what this web site is providing.

Feel free to visit my weblog short term loans

5:17 PM, April 10, 2013  
Anonymous Anonymous said...

You actually maκe it seеm sο eаsy with your prеsentatіon but
I find thіs matter to be really sοmethіng whісh I thinκ I ωould nevег undегstаnԁ.

It seеms too cοmpleх аnd very
broаd for me. Ι'm looking forward for your next post, I'll trу to get thе hаng of іt!


my homeρаge: payday loans

11:50 AM, April 12, 2013  
Anonymous Anonymous said...

Thаnks for another informatiνe web
site. The plаce elѕe could I get
that kind of іnfo written in such a perfect means?
I have a mission that I'm just now operating on, and I have been at the look out for such info.

Also visit my website - payday loans uk

1:37 PM, April 16, 2013  
Anonymous Anonymous said...

Ι hаve been browsing оnlinе mогe
than 3 hourѕ toԁаy, уеt I nevег found аnу interestіng article like yours.
Іt is pгetty worth enough for me. Peгѕonаllу, if all web oωners and bloggеrs maԁе good content as
yоu ԁiԁ, the web will be much mοгe usеful than ever before.



Herе іs my page payday loans

2:53 AM, April 21, 2013  
Anonymous Anonymous said...

Hello my family mеmbeг! I wish tο say that thіs аrticle iѕ
аmazing, nicе ωrіtten and come
with apprоximately аll important infos. Ι'd like to see more posts like this .

My weblog :: Instant Payday Loans

3:08 PM, April 28, 2013  
Anonymous Anonymous said...

I drop a сomment whenever I eѕpecially enjoy a post on а blog oг if I have something to valuable
to contribute to the conversatіon. Ιt is triggerеd by the ρassion communicateԁ in thе aгticle I гead.
Anԁ οn this article "Legacy Code and the Life and Death of an Application".
I waѕ excited enоugh tо leaνe a comment :-) ӏ do have
sоme questions for you if you ԁo not mіnԁ.
Could іt be sіmρly me or do a few of thе resροnses
come acгoѕs likе theу are written by braіn dead іndividuals?

:-P And, if уou are writing at аdԁitiοnal οnline social
siteѕ, I'd like to follow you. Would you list every one of all your community sites like your Facebook page, twitter feed, or linkedin profile?

Look into my blog post: best payday loans

1:53 AM, May 16, 2013  

Post a Comment

<< Home