taming the monster legacy code beast

Post on 10-May-2015

1.822 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Does that legacy code beast stifle your creativity? Do you stay up late at night frightened that someone might touch *that* piece of code? Learn some practical steps on how you can slay the legacy code beasts, divide and conquer, and deliver value to your customers.

TRANSCRIPT

PROACTION

M E N T O R S

Taming Legacy Code Monsters

PROACTION

M E N T O R S

Caleb *

* I’m here to help.

PROACTION

M E N T O R S

PROACTION

M E N T O R S

10 Reasons your software Sucks(and the 10 practices to fix it)

Last year

PROACTION

M E N T O R S

DevelopmentObject Orientation

SOLID

Patterns

Secure Coding

Team WorkAutomated Tests

Source Control

Automated Builds

ProcessAgile, Lean, XP

Team Dynamics

Continuous Learning

Bui ld ing teams that engineer better software

Development ∙ Team Work ∙ Process

PROACTION

M E N T O R S

back to…

Taming Legacy Code Monsters

PROACTION

M E N T O R S

back to…

Taming Legacy Code Monsters

PROACTION

M E N T O R S

Definition “Legacy”n. Software that I’m afraid to maintain,

extend, or change it actually scares me to

touch that code.

PROACTION

M E N T O R S

What’s the difference between an application and

UI Auth Validation

Rules Workflow Processor

Services Data Cache

PROACTION

M E N T O R S

Legacy Monster

UI Auth Validation

Rules Workflow Processor

Services Data Cache

PROACTION

M E N T O R S

Legacy MonsterStatic ConfigStatic ProxyProcessor SettingsData ConnectionsRules Configurations

Rules

Processor

Data

Coupling

Cou

plin

g

Coupling

Coupling

CouplingCoupling

PROACTION

M E N T O R S

Legacy MonsterSolution contained over 68 projects.

Most of which were not used.

No horizontal layers.

Required IIS 6 / legacy URL rewriter / 32 bit C Lib / IIS Rules export/import /

Database Connections / Proper Configuration X 3

To run or test ANY part on a dev box…

PROACTION

M E N T O R S

Know your FoeStra

tegy #1

PROACTION

M E N T O R S

nDepend

Strategy #1

PROACTION

M E N T O R S

Visual Studio Dependency Graph

Strategy #1

PROACTION

M E N T O R S

Strategy #1

DemoUsing theVisual Studio Dependency Graph

PROACTION

M E N T O R S

Pro tip:Look for big blobs & thick lines to concrete types

Strategy #1

Demo

PROACTION

M E N T O R S

Strategy #2

Create a

Safety Net

PROACTION

M E N T O R S

Strategy #2

Source Control+ Test

PROACTION

M E N T O R S

Strategy #2

Source Control+ Test

PROACTION

M E N T O R S

Strategy #2

Source Control

PROACTION

M E N T O R S

// source// trunk

// r1 // r2 // r3

// r1.1

// trunk

//

// svn / git / tfs ..

Branching for FUN!

PROACTION

M E N T O R S

Strategy #2

Tests

Unit TestsIntegrationRegressionLoad TestsEtc…

PROACTION

M E N T O R S

Isolation and White box Unit Testing for .NET

Legacy MonsterStatic ConfigStatic IProxyProcessor SettingsData ConnectionsRules Configurations

Rules

Processor

Data

“Pex automatically 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.”

http://research.microsoft.com/en-us/projects/pex/

Demo: Safety Net

PROACTION

M E N T O R S

what about hard to test code?refactoring testability

PROACTION

M E N T O R S

private IList<Bean> goodBeans { get; set;}private IList<Bean> badBeans { get; set;}

public void AddBean(Bean bean){

if (bean.size > 10)goodBeans.Add(bean);

elsebadBeans.Add(bean);

}public void UpdateBeans(){ BeanData.Update(goodBeans); goodBeans.Clear(); badBeans.Clear();}

PROACTION

M E N T O R S

Clearly an arbitrary example1Does represent actual production code that I have worked with

Usage scenario is2AddBean(b); AddBean(b), UpdateBeans();

How can we test the AddBean() Logic?3

PROACTION

M E N T O R S

private IList<Bean> goodBeans { get; set;}private IList<Bean> badBeans { get; set;}

public void AddBean(Bean bean){

if (bean.size > 10)goodBeans.Add(bean);

elsebadBeans.Add(bean);

}public void UpdateBeans(){ BeanData.Update(goodBeans); goodBeans.Clear(); badBeans.Clear();}

public IList<Bean> GoodBeans { get; private set;}private Ilist <Bean>badBeans { get; set;}

public void AddBean(Bean bean){

if (bean.size > 10)GoodBeans.Add(bean);

elsebadBeans.Add(bean);

}public void UpdateBeans(){ BeanData.Update(goodBeans); GoodBeans.Clear(); badBeans.Clear();}

A: create a readonlyaccessor to “sense” thegoodBean state externally

PROACTION

M E N T O R S

public void UpdateBeans(){ BeanData.Update(goodBeans);

goodBeans.Clear(); badBeans.Clear();

}

What about the UpdateBeans() method?public void UpdateBeans(){ BeanData.Update(goodBeans);

goodBeans.Clear(); badBeans.Clear();

if (OnBeansUpdate != null) OnBeansUpdate();}

public event Action OnBeansUpdate;

PROACTION

M E N T O R S

Not a silver bullet

still need to make sure that we are testing “the right” things, and not just hitting some magical number

still need to make sure we have healthy tests

Code Coverage + Continuous Integration

Pro tip

PROACTION

M E N T O R S

Code Coverage + Continuous IntegrationCreates a base line or smoke test to look for

Watch trends over magic percent numbers

Pro tip

PROACTION

M E N T O R S

Code Coverage + Continuous IntegrationTeam City Code Coverage

PROACTION

M E N T O R S

Code Coverage + Continuous IntegrationSonar

PROACTION

M E N T O R S

Strategy #3

PROACTION

M E N T O R S

Strategy #3

Divide and Conquer

(pick your battles)

PROACTION

M E N T O R S36

Commitment

PROACTION

M E N T O R S37

Commitment Raise your hand and repeat after me,

PROACTION

M E N T O R S38

Commitment Raise your hand and repeat after me,

I will not practice bad code.

PROACTION

M E N T O R S

PROACTION

M E N T O R S

“Resist Legacy Coupling”

PROACTION

M E N T O R S

“Resist Legacy Coupling”Legacy Monster

Static ConfigStatic ProxyProcessor SettingsData ConnectionsRules Configurations

Rules

Processor

Data

Coupling

Cou

plin

g

CouplingC

oupling CouplingCoupling

tight coupling = change anywhereaffects everything

New Functionality

Needs IProxy

don’t reference Config Monster

use DI for only what you need (IProxy)

static Config class leads to Heavy Weight testing

PROACTION

M E N T O R S

Introduce SOLID principals on new development… gradually improve code over time

http://developingux.com/di/

PROACTION

M E N T O R S

wrap up

PROACTION

M E N T O R S

wrap upKnow your Foe

Use a Safety Net

Divide and Conquer

PROACTION

M E N T O R S

@calebjenkins

@proactionmentor

http://developingux.com

caleb@proactionmentors.com

Thank you!

PROACTION

M E N T O R S

PROACTION

M E N T O R S

Photo Credits

http://www.flickr.com/photos/boogeyman13/2432667459/

http://www.flickr.com/photos/stopthegears/50553611/

http://www.flickr.com/photos/pochateca/324414459

http://www.flickr.com/photos/27620885@N02/2594362917

© Michael Cummings - http://dreaminpictures.com

PROACTION

M E N T O R S

http://www.flickr.com/photos/flyingturtle/3976070342/

http://www.lostechies.com/blogs/derickbailey/archive/2009/02/11/solid-development-principles-in-motivational-pictures.aspx

http://www.flickr.com/photos/jfravel/1001472806/

http://www.flickr.com/photos/flyingturtle/3976076272/

http://www.flickr.com/photos/27620885@N02/2594363003

top related