Taming the Monster Legacy Code Beast
- 12. 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…
- 22. // source// svn / git / tfs .. // r1.1// r1// r2// r3// trunk// trunkBranching for FUN!//
- 24. 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
- 26. 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();}
- 27. 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?
- 28. 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
- 29. 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;
- 30. 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
- 31. Code Coverage + Continuous IntegrationCreates a base line or smoke test to look forWatch trends over magic percent numbersPro tip
- 37. 37 Raise your hand and repeat after me,Commitment
- 38. 38 Raise your hand and repeat after me,CommitmentI will not practice bad code.
- 41. “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)
- 46. 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
Editor's Notes
- take a minute to have student “read” test and talk out what it’s doing.
- Build out slide.. Before you click through: take a minute to have student “read” test and talk out what it’s doing.