working effectively with legacy code

20
Working Effectively with Legacy Code Roberto Hernandez-Pou Managing Consultant Excella Consulting @hernandezrobert http://www.overridethis.com Excella Consulting – http://www.excella.com

Upload: excella-consulting

Post on 09-May-2015

1.067 views

Category:

Technology


2 download

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

Page 1: Working Effectively With Legacy Code

Working Effectively with Legacy Code

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

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

Page 2: Working Effectively With Legacy Code

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

Page 3: Working Effectively With Legacy Code

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

Page 4: Working Effectively With Legacy Code

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

Page 5: Working Effectively With Legacy Code

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

Page 6: Working Effectively With Legacy Code

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

Page 7: Working Effectively With Legacy Code

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

Page 8: Working Effectively With Legacy Code

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

Page 9: Working Effectively With Legacy Code

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

Page 10: Working Effectively With Legacy Code

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

Page 11: Working Effectively With Legacy Code

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

Page 12: Working Effectively With Legacy Code

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

Page 13: Working Effectively With Legacy Code

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

Page 14: Working Effectively With Legacy Code

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

Page 15: Working Effectively With Legacy Code

Tools

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

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

Page 16: Working Effectively With Legacy Code

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

Page 17: Working Effectively With Legacy Code

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

Page 18: Working Effectively With Legacy Code

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!

Page 19: Working Effectively With Legacy Code

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

Page 20: Working Effectively With Legacy Code

Thank You!

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

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

2.Email: [email protected]

3. @hernandezrobert

4.