working effectively with legacy code

Post on 09-May-2015

1.067 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

Working Effectively with Legacy Code was presented at the 2012 DC Agile Engineering Conference on 12/7/2012 by Excella Managing Consultant Roberto Hernandez-Pou (@hernandezrobert).

TRANSCRIPT

Working Effectively with Legacy Code

Roberto Hernandez-PouManaging ConsultantExcella Consulting@hernandezroberthttp://www.overridethis.com

Excella Consulting – http://www.excella.com

Legacy Code: Characteristics

• Poor Architecture• Non-Uniform coding styles• Poor or Non-Existing Documentation• Mythical “Oral” Documentation• No Tests (or Minimal Test Coverage)

• Extremely Valuable!– Only Successful code becomes Legacy

Code.

Excella Consulting – http://www.excella.com#DCAEC12 @HernandezRobert 

* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers

Agenda

• How does code become Legacy Code?• Reasons to work with Legacy Code• What are my options?• How do we approach the work?

– Edit and Pray ‘or’ Cover and Modify

• The Legacy Code Change Algorithm• Sensing and Separation• Fake Collaborators• Seams• Tools• TDD for Legacy Code

Excella Consulting – http://www.excella.com#DCAEC12 @HernandezRobert 

* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers

How does code become Legacy Code?

• New Features or New Requirements– Developers usually turn to shortcuts and hacks to make

deadlines and deliver on time to market.

• Software Team Turnover or Large Development Teams– Multiple styles of coding. – Lack of vision.– Code duplication.

• Poor Team Communication.

Excella Consulting – http://www.excella.com#DCAEC12 @HernandezRobert 

* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers

Reasons to “Work with Legacy Code”

• Four reasons to change software:– Adding a feature– Fixing a bug– Refactoring– Optimizing resource usage

Adding a Feature Fixing a Bug Refactoring Optimizing resource usage

Structure Changes Changes Changes

New Functionality Changes

Functionality Changes

Resource Usage Changes

Risk!

Excella Consulting – http://www.excella.com#DCAEC12 @HernandezRobert 

* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers

As a Developer what are my options?

• Look for a new Job?• Start from scratch.

– Big bang approach!

• Have somebody else do the job:– Members of the original development team – Consultants ($$$)

• Deal with it!

Excella Consulting – http://www.excella.com#DCAEC12 @HernandezRobert 

* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers

How do we approach the work?

• Working with Feedback– Edit and pray

(Industry standard)

– Cover and modify

Excella Consulting – http://www.excella.com#DCAEC12 @HernandezRobert 

* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers

Edit and Pray

• Carefully plan the changes• Make sure you understand the code• Start making changes• Poke around to see if you broke anything• Deploy to production environment• Wait for feedback from customers

Cover and Modify

• Build a safety net (Covering = Testing)• Unit Testing• Higher level testing

The Legacy Code DilemmaWhen we change code, we should have tests in place.

To put tests in place we need to change code

Excella Consulting – http://www.excella.com#DCAEC12 @HernandezRobert 

* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers

Cover and Modify

• Introduce Unit Tests

• Unit Tests are:– Fast– Localise Problems– Do not talk to a database– Do not communicate over the network– Do not communicate with local resources– Do not need config changes

Excella Consulting – http://www.excella.com#DCAEC12 @HernandezRobert 

* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers

The Legacy Code Change Algorithm

Identify change points Find test points Break dependencies Write tests Make changes and refactor

Excella Consulting – http://www.excella.com#DCAEC12 @HernandezRobert 

* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers

Sensing & Separation

• Sensing: we break dependencies to sense when we can't access values our code computes

• Separation: we break dependencies to separate when we can't even get a piece of code into a test harness to run

Excella Consulting – http://www.excella.com#DCAEC12 @HernandezRobert 

* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers

Fake Collaborators

• Object that impersonates some collaborator of your class when its being tested.– Demo: Echo Console

Excella Consulting – http://www.excella.com#DCAEC12 @HernandezRobert 

* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers

The Seam Model

• Seam: a place where you can alter behavior of your program without editing in that place.

• Enabling Point: Every seam has an enabling point, a place where you can make the decision to use one behavior or another.

• Looking for existing seams in legacy code allow us to break dependencies (for sensing or separation) without refactoring.

• Demo: Echo Console• Demo: Fake Data Reader

Excella Consulting – http://www.excella.com#DCAEC12 @HernandezRobert 

* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers

Tools

Excella Consulting – http://www.excella.com#DCAEC12 @HernandezRobert 

* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers

Test Driven Development

• Test-Driven Development (TDD)

1. Write a failing test2. Get it to compile3. Make it pass 4. Remove duplication 5. Repeat

• Programming by difference

Excella Consulting – http://www.excella.com#DCAEC12 @HernandezRobert 

* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers

Test Driven Development

• Test-Driven Development (TDD) for legacy

0. Get the class you want to change under test1. Write a failing test2. Get it to compile3. Make it pass (try not to change existing code)4. Remove duplication 5. Repeat

• Programming by difference

Excella Consulting – http://www.excella.com#DCAEC12 @HernandezRobert 

* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers

Conclusions

• No “silver bullet” here, it's hard work but not impossible • Overwhelming at first but things will get better as the

number of tests increase• Be pragmatic!

Resources

Excella Consulting – http://www.excella.com

1.“Working Effectively with Legacy Code” by Michael Feathers

2. http://www.objectmentor.com

3. http://www.overridethis.com

4.http://bitbucket.org/rhp_74/overridethis.legacycodesample

Thank You!

Excella Consulting – http://www.excella.com

1. Blog: http://www.overridethis.com

2.Email: roberto.hernandez@excella.com

3. @hernandezrobert

4.

top related