SlideShare a Scribd company logo
Working Effectively with
  Legacy Code
                     Roberto Hernandez-Pou
                     Managing Consultant
                     Excella Consulting
                     @hernandezrobert
                     http://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


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 Dilemma
                      When 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 test
       2. Get it to compile
       3. 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 test
       1. Write a failing test
       2. Get it to compile
       3. 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


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

   2.      http://www.objectmentor.com

   3.      http://www.overridethis.com

           http://bitbucket.org/rhp_74/overridethis.legacycode
   4.
           sample

Excella Consulting – http://www.excella.com
Thank You!


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

   2.      Email: roberto.hernandez@excella.com

   3.      @hernandezrobert

   4.


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

More Related Content

PDF
Get the Look and Feel You Want in Oracle APEX
PDF
5 physical data modeling blunders 09092010
PPTX
Automated Testing with Databases
PPTX
The Fundamentals of Continuous Software Design
PPTX
Automated Testing: Obstacles, Pitfalls, and Dangers
PPTX
Getting started with Couchbase
PPTX
The Five Ways of Building Oracle Applications
PPTX
Embracing OSS in the enterprise
Get the Look and Feel You Want in Oracle APEX
5 physical data modeling blunders 09092010
Automated Testing with Databases
The Fundamentals of Continuous Software Design
Automated Testing: Obstacles, Pitfalls, and Dangers
Getting started with Couchbase
The Five Ways of Building Oracle Applications
Embracing OSS in the enterprise

Viewers also liked (20)

PPTX
чужинова нв, шляхтина он
PPTX
Destiny’s christmas speech
PDF
Scatol8 UUDD livello A_2
PPTX
自己的遊戲自己寫 - 立志中學 2014/12/1 遊戲設計分享簡報
DOCX
PPTX
عرض الدرعية
DOC
DOCX
PDF
La crudelta
DOC
PPTX
Coffee Shop Security Group Tool
PDF
[CLP] IT industry Day 2 - 4
PPT
Discover the power to be truly
PPTX
Presentatie VOV- retentie
PPTX
Feliz dia del maestro.pptx rr
PPT
Villast longani
PDF
Larry Logan Live Webinar: Winning Customer Loyalty Through Digitally Empowere...
 
PPTX
Employment Testing in HRM.
PPTX
Teorías de la Psicología Educativa
PPTX
Trabajo grupal
чужинова нв, шляхтина он
Destiny’s christmas speech
Scatol8 UUDD livello A_2
自己的遊戲自己寫 - 立志中學 2014/12/1 遊戲設計分享簡報
عرض الدرعية
La crudelta
Coffee Shop Security Group Tool
[CLP] IT industry Day 2 - 4
Discover the power to be truly
Presentatie VOV- retentie
Feliz dia del maestro.pptx rr
Villast longani
Larry Logan Live Webinar: Winning Customer Loyalty Through Digitally Empowere...
 
Employment Testing in HRM.
Teorías de la Psicología Educativa
Trabajo grupal
Ad

Similar to Working with Legacy Code (20)

PDF
Ekon20 mORMot Legacy Code Technical Debt Delphi Conference
PDF
Create Your Own Starter Files
PDF
Agile, Automated, Aware: How to Model for Success
PPTX
Eurosport's Kodakademi #2
PPTX
Top 10 Reasons to Switch to ER/Studio
PDF
SE2016 Java Alex Theedom "Java EE revisits design patterns"
PDF
Alex Theedom Java ee revisits design patterns
PPTX
14 Habits of Great SQL Developers
PPTX
Data Modeling for NoSQL
PDF
How to guarantee your change is integrated to Moodle core
PPTX
TDD and the Legacy Code Black Hole
PPTX
14 Habits of Great SQL Developers
PPTX
Intelligent content and Flare
PPTX
Art of refactoring - Code Smells and Microservices Antipatterns
PDF
Puppet Camp Austin 2015: Getting Started with Puppet
PPT
SE2016 - Java EE revisits design patterns 2016
PDF
Building a Collaborative Data Architecture
PDF
4 roles on the it project team
PPTX
Clean code presentation
PPTX
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
Ekon20 mORMot Legacy Code Technical Debt Delphi Conference
Create Your Own Starter Files
Agile, Automated, Aware: How to Model for Success
Eurosport's Kodakademi #2
Top 10 Reasons to Switch to ER/Studio
SE2016 Java Alex Theedom "Java EE revisits design patterns"
Alex Theedom Java ee revisits design patterns
14 Habits of Great SQL Developers
Data Modeling for NoSQL
How to guarantee your change is integrated to Moodle core
TDD and the Legacy Code Black Hole
14 Habits of Great SQL Developers
Intelligent content and Flare
Art of refactoring - Code Smells and Microservices Antipatterns
Puppet Camp Austin 2015: Getting Started with Puppet
SE2016 - Java EE revisits design patterns 2016
Building a Collaborative Data Architecture
4 roles on the it project team
Clean code presentation
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
Ad

More from DC Agile Engineering Conference (6)

PPTX
Unindented Code Cannot Possibly Work
PPTX
PPTX
Continuous Integration: Toolbox, Blueprint, Master Craft
PPTX
Continuous Delivery Applied
PDF
Agile Engineering and Software Craftsmanship
PPTX
Improving Design through TDD
Unindented Code Cannot Possibly Work
Continuous Integration: Toolbox, Blueprint, Master Craft
Continuous Delivery Applied
Agile Engineering and Software Craftsmanship
Improving Design through TDD

Recently uploaded (20)

PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Cloud computing and distributed systems.
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Machine learning based COVID-19 study performance prediction
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Big Data Technologies - Introduction.pptx
PPT
Teaching material agriculture food technology
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Spectral efficient network and resource selection model in 5G networks
CIFDAQ's Market Insight: SEC Turns Pro Crypto
MYSQL Presentation for SQL database connectivity
Cloud computing and distributed systems.
“AI and Expert System Decision Support & Business Intelligence Systems”
Dropbox Q2 2025 Financial Results & Investor Presentation
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Machine learning based COVID-19 study performance prediction
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation theory and applications.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
20250228 LYD VKU AI Blended-Learning.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
A Presentation on Artificial Intelligence
Empathic Computing: Creating Shared Understanding
Understanding_Digital_Forensics_Presentation.pptx
Big Data Technologies - Introduction.pptx
Teaching material agriculture food technology

Working with Legacy Code

  • 1. Working Effectively with Legacy Code Roberto Hernandez-Pou Managing Consultant Excella Consulting @hernandezrobert http://www.overridethis.com Excella Consulting – http://www.excella.com
  • 2. 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
  • 3. 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
  • 4. 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
  • 5. 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 Excella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert * Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. Cover and Modify • Build a safety net (Covering = Testing) • Unit Testing • Higher level testing The Legacy Code Dilemma When 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
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. 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
  • 15. Tools Excella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert * Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
  • 16. Test Driven Development • Test-Driven Development (TDD) 1. Write a failing test 2. Get it to compile 3. 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
  • 17. Test Driven Development • Test-Driven Development (TDD) for legacy 0. Get the class you want to change under test 1. Write a failing test 2. Get it to compile 3. 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
  • 18. 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!
  • 19. Resources “Working Effectively with Legacy Code” by 1. Michael Feathers 2. http://www.objectmentor.com 3. http://www.overridethis.com http://bitbucket.org/rhp_74/overridethis.legacycode 4. sample Excella Consulting – http://www.excella.com
  • 20. Thank You! 1. Blog: http://www.overridethis.com 2. Email: roberto.hernandez@excella.com 3. @hernandezrobert 4. Excella Consulting – http://www.excella.com