SlideShare a Scribd company logo
Taming Legacy Code Monsters
*Caleb* I’m here to help.
Taming the Monster Legacy Code Beast
Last year10Reasons yoursoftwareSucks(and the 10 practicesto fix it)
Building teams that engineer better softwareDevelopment ∙ Team Work ∙ Process
back to…Taming Legacy Code Monsters
back to…Taming Legacy Code Monsters
Definition “Legacy”n. Software that I’m afraid to maintain, extend, or change it actually scares me to touch that code.
What’s the differencebetween an application and
Legacy Monster
Legacy MonsterProcessorStatic ConfigStatic ProxyProcessor SettingsData ConnectionsRules ConfigurationsCouplingCouplingCouplingCouplingCouplingRulesDataCoupling
Legacy MonsterSolution contained over 68 projects.Most of which were notused.No horizontal layers.Required IIS 6 / legacy URL rewriter / 32 bit C Lib / IIS Rules export/import / Database Connections / Proper Configuration X 3To run or test ANY part on a dev box…
Strategy #1Know your Foe
Strategy #1nDepend
Strategy #1Visual Studio Dependency Graph
Strategy #1DemoUsing theVisual Studio Dependency Graph
Strategy #1DemoPro tip:Look for big blobs& thick linesto concrete types
Strategy #2Create aSafety Net
Source Control+ TestStrategy #2
Strategy #2Source Control+ Test
Strategy #2Source Control
// source// svn / git / tfs  .. // r1.1// r1// r2// r3// trunk// trunkBranching for FUN!//
Strategy #2Unit TestsIntegrationRegressionLoad TestsEtc…Tests
Isolation and White box Unit Testing for .NETDemo: Safety NetLegacy Monster“Pexautomatically generates test suites with high code coverage.…[and] finds interesting input-output values of your methods, which you can save as a small test suite with high code coverage. Microsoft Pex is a Visual Studio add-in for testing .NET Framework applications.”ProcessorStatic ConfigStatic IProxyProcessor SettingsData ConnectionsRules Configurationshttp://research.microsoft.com/en-us/projects/pex/RulesData
what about hard to test code?refactoring testability
privateIList<Bean> goodBeans { get; set;}privateIList<Bean> badBeans { get; set;}publicvoidAddBean(Bean bean){	if(bean.size > 10)goodBeans.Add(bean);	elsebadBeans.Add(bean);}publicvoidUpdateBeans(){BeanData.Update(goodBeans);goodBeans.Clear();badBeans.Clear();}
1Clearly an arbitrary exampleDoes represent actual production code that I have worked with23AddBean(b); AddBean(b), UpdateBeans();Usage scenario isHow can we test the AddBean() Logic?
publicIList<Bean> GoodBeans{ get; private set;}privateIlist <Bean>badBeans{ get; set;}publicvoidAddBean(Bean bean){if(bean.size > 10)GoodBeans.Add(bean);elsebadBeans.Add(bean);}publicvoidUpdateBeans(){BeanData.Update(goodBeans);GoodBeans.Clear();badBeans.Clear();}privateIList<Bean> goodBeans { get; set;}privateIList<Bean> badBeans { get; set;}publicvoidAddBean(Bean bean){	if(bean.size > 10)goodBeans.Add(bean);	elsebadBeans.Add(bean);}publicvoidUpdateBeans(){BeanData.Update(goodBeans);goodBeans.Clear();badBeans.Clear();}A: create a readonlyaccessor to “sense” thegoodBean state externally
What about the UpdateBeans() method?publicvoidUpdateBeans(){BeanData.Update(goodBeans);goodBeans.Clear();badBeans.Clear();}publicvoidUpdateBeans(){BeanData.Update(goodBeans);goodBeans.Clear();badBeans.Clear();if (OnBeansUpdate != null)OnBeansUpdate();}publiceventActionOnBeansUpdate;
Code Coverage + Continuous IntegrationNot a silver bulletstill need to make sure that we are testing “the right” things, and not just hitting some magical numberstill need to make sure we have healthy testsPro tip
Code Coverage + Continuous IntegrationCreates a base line or smoke test to look forWatch trends over magic percent numbersPro tip
Code Coverage + Continuous IntegrationTeam City Code Coverage
Code Coverage + Continuous IntegrationSonar
Strategy #3
Strategy #3Divide and Conquer (pick your battles)
36Commitment
37                           Raise your hand and repeat after me,Commitment
38                           Raise your hand and repeat after me,CommitmentI will not practice bad code.
Taming the Monster Legacy Code Beast
“Resist Legacy Coupling”
“Resist Legacy Coupling”tight coupling = change anywhereaffects everythingLegacy MonsterProcessorStatic ConfigStatic ProxyProcessor SettingsData ConnectionsRules ConfigurationsNew FunctionalityCouplingNeeds IProxyCouplingstatic Config class leads to Heavy Weight testingCouplingCouplingCouplingRulesDatadon’t reference Config MonsterCouplinguse DI for only what you need (IProxy)
Introduce SOLID principals on new development… gradually improve code over timehttp://developingux.com/di/
wrap up
Know your Foe wrap upUse a Safety NetDivide and  Conquer
Thank you!ProactionMentorS@calebjenkinshttp://developingux.com@proactionmentorcaleb@proactionmentors.com
Photo Credits© Michael Cummings - http://dreaminpictures.comhttp://www.flickr.com/photos/boogeyman13/2432667459/http://www.flickr.com/photos/stopthegears/50553611/http://www.flickr.com/photos/pochateca/324414459http://www.flickr.com/photos/27620885@N02/2594362917
http://www.flickr.com/photos/27620885@N02/2594363003http://www.flickr.com/photos/flyingturtle/3976070342/http://www.flickr.com/photos/jfravel/1001472806/http://www.flickr.com/photos/flyingturtle/3976076272/http://www.lostechies.com/blogs/derickbailey/archive/2009/02/11/solid-development-principles-in-motivational-pictures.aspx

More Related Content

Taming the Monster Legacy Code Beast

Editor's Notes

  1. take a minute to have student “read” test and talk out what it’s doing.
  2. Build out slide.. Before you click through: take a minute to have student “read” test and talk out what it’s doing.