SlideShare une entreprise Scribd logo
1  sur  14
Testing and Refactoring
      Legacy Code



               Sandro Mancuso
               @sandromancuso
               craftedsw.blogspot.com
What is this session about?
Business Requirements
Imagine a social networking website for
travellers

• You need to be logged in to see the content
• You need to be a friend to see someone else’s
  trips
Legacy Code Rules
• You cannot change production code if not
  covered by tests
  – Just automated refactorings (via IDEs) are allowed,
    if needed to write the test
Tips
• Use a code coverage tool
• Commit as often as possible
  – Enables you to commit (push) improvements
    quickly
  – Allows you to roll back (reset) if you get lost
• When refactoring, try to stay in the green for
  as long as possible
Exercise
Working with Legacy Code Tips




                               Start refactoring from
                               deepest to shortest
                               branch

Start testing from
shortest to
deepest branch
Trip Service - Problems

                                                 <<Singleton>>
                                                  UserSession
                                      +getInstance() : UserSession
                                      +getLoggedUser() : User
TripService
+findTripsByUser(User) : List<Trip>

                                                    TripDAO
                                      +findTripsByUser(User) : List<Trip>
Legacy Code Common Problems
• Hard-wired dependencies
  – Create a “seam” to break dependencies
     • Extract dependency in a protected method
     • In our test, extend the class under test and override the
       method with the dependency so you can mock the
       result
• Identify feature envy
  – Move behaviour out to the appropriate class
Exercise
Remember




                                Start refactoring from
                                deepest to shortest
                                branch

Start testing from
shortest to
deepest branch
Refactoring vs Re-design
• After localised refactoring, consider fixing the
  design
• Writing tests for existing code may perpetuate
  a bad design
Craftsmen at work
• Write readable and maintainable code
  – Code must express business rules
• Strive for simplicity
• Know your tools well (i.e. frameworks, shortcuts)
• Work in small and safe increments
  – Commit often
• Embrace changes, be brave
• Boy scout rule / No broken windows
Thanks
https://github.com/sandromancuso/trip-service-kata



                               Sandro Mancuso
                               @sandromancuso
                               craftedsw.blogspot.com

Contenu connexe

Tendances

Cqrs and Event Sourcing Intro For Developers
Cqrs and Event Sourcing Intro For DevelopersCqrs and Event Sourcing Intro For Developers
Cqrs and Event Sourcing Intro For Developerswojtek_s
 
Architectural patterns part 1
Architectural patterns part 1Architectural patterns part 1
Architectural patterns part 1assinha
 
Software Architecture: Principles, Patterns and Practices
Software Architecture: Principles, Patterns and PracticesSoftware Architecture: Principles, Patterns and Practices
Software Architecture: Principles, Patterns and PracticesGanesh Samarthyam
 
Software Design
Software DesignSoftware Design
Software DesignHa Ninh
 
Testing database applications with QuickCheck
Testing database applications with QuickCheckTesting database applications with QuickCheck
Testing database applications with QuickCheckLaura M. Castro
 
Lecture 14 requirements modeling - flow and behavior
Lecture 14   requirements modeling - flow and  behaviorLecture 14   requirements modeling - flow and  behavior
Lecture 14 requirements modeling - flow and behaviorIIUI
 
Architectural Design Pattern: Android
Architectural Design Pattern: AndroidArchitectural Design Pattern: Android
Architectural Design Pattern: AndroidJitendra Kumar
 
LINQ 2 SQL Presentation To Palmchip And Trg, Technology Resource Group
LINQ 2 SQL Presentation To Palmchip  And Trg, Technology Resource GroupLINQ 2 SQL Presentation To Palmchip  And Trg, Technology Resource Group
LINQ 2 SQL Presentation To Palmchip And Trg, Technology Resource GroupShahzad
 
Analysis concepts and principles
Analysis concepts and principlesAnalysis concepts and principles
Analysis concepts and principlessaurabhshertukde
 
Why do complex software application projects drag?
Why do complex software application projects drag?Why do complex software application projects drag?
Why do complex software application projects drag?Stephen Erdman
 

Tendances (19)

Cqrs and Event Sourcing Intro For Developers
Cqrs and Event Sourcing Intro For DevelopersCqrs and Event Sourcing Intro For Developers
Cqrs and Event Sourcing Intro For Developers
 
Architectural patterns part 1
Architectural patterns part 1Architectural patterns part 1
Architectural patterns part 1
 
Software Patterns
Software PatternsSoftware Patterns
Software Patterns
 
Software Architecture: Principles, Patterns and Practices
Software Architecture: Principles, Patterns and PracticesSoftware Architecture: Principles, Patterns and Practices
Software Architecture: Principles, Patterns and Practices
 
Software Design
Software DesignSoftware Design
Software Design
 
Testing database applications with QuickCheck
Testing database applications with QuickCheckTesting database applications with QuickCheck
Testing database applications with QuickCheck
 
Slides chapter 11
Slides chapter 11Slides chapter 11
Slides chapter 11
 
Lecture 14 requirements modeling - flow and behavior
Lecture 14   requirements modeling - flow and  behaviorLecture 14   requirements modeling - flow and  behavior
Lecture 14 requirements modeling - flow and behavior
 
Architectural Design Pattern: Android
Architectural Design Pattern: AndroidArchitectural Design Pattern: Android
Architectural Design Pattern: Android
 
Facade Design Pattern
Facade Design PatternFacade Design Pattern
Facade Design Pattern
 
LINQ 2 SQL Presentation To Palmchip And Trg, Technology Resource Group
LINQ 2 SQL Presentation To Palmchip  And Trg, Technology Resource GroupLINQ 2 SQL Presentation To Palmchip  And Trg, Technology Resource Group
LINQ 2 SQL Presentation To Palmchip And Trg, Technology Resource Group
 
Facadepattern
FacadepatternFacadepattern
Facadepattern
 
Day4
Day4Day4
Day4
 
Facade pattern
Facade patternFacade pattern
Facade pattern
 
Analysis concepts and principles
Analysis concepts and principlesAnalysis concepts and principles
Analysis concepts and principles
 
Uml intro
Uml introUml intro
Uml intro
 
Scala meetup - Objectify
Scala meetup - ObjectifyScala meetup - Objectify
Scala meetup - Objectify
 
Design final
Design finalDesign final
Design final
 
Why do complex software application projects drag?
Why do complex software application projects drag?Why do complex software application projects drag?
Why do complex software application projects drag?
 

Similaire à Legacy Code Hands-on Session

Sandro Mancuso – Testing and refactoring legacy code @ I T.A.K.E. Unconferenc...
Sandro Mancuso – Testing and refactoring legacy code @ I T.A.K.E. Unconferenc...Sandro Mancuso – Testing and refactoring legacy code @ I T.A.K.E. Unconferenc...
Sandro Mancuso – Testing and refactoring legacy code @ I T.A.K.E. Unconferenc...Mozaic Works
 
Refactoring ASP.NET and beyond
Refactoring ASP.NET and beyondRefactoring ASP.NET and beyond
Refactoring ASP.NET and beyondDotNetMarche
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agilityelliando dias
 
The Diabolical Developers Guide to Performance Tuning
The Diabolical Developers Guide to Performance TuningThe Diabolical Developers Guide to Performance Tuning
The Diabolical Developers Guide to Performance TuningjClarity
 
2011-02-03 LA RubyConf Rails3 TDD Workshop
2011-02-03 LA RubyConf Rails3 TDD Workshop2011-02-03 LA RubyConf Rails3 TDD Workshop
2011-02-03 LA RubyConf Rails3 TDD WorkshopWolfram Arnold
 
The View - Lotusscript coding best practices
The View - Lotusscript coding best practicesThe View - Lotusscript coding best practices
The View - Lotusscript coding best practicesBill Buchan
 
gDayX 2013 - Advanced AngularJS - Nicolas Embleton
gDayX 2013 - Advanced AngularJS - Nicolas EmbletongDayX 2013 - Advanced AngularJS - Nicolas Embleton
gDayX 2013 - Advanced AngularJS - Nicolas EmbletonGeorge Nguyen
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchMats Bryntse
 
Groovy In the Cloud
Groovy In the CloudGroovy In the Cloud
Groovy In the CloudJim Driscoll
 
Test driven development v1.0
Test driven development v1.0Test driven development v1.0
Test driven development v1.0Ganesh Kondal
 
Behavior Driven Development by Example
Behavior Driven Development by ExampleBehavior Driven Development by Example
Behavior Driven Development by ExampleNalin Goonawardana
 
Test Driven Development Introduction
Test Driven Development IntroductionTest Driven Development Introduction
Test Driven Development IntroductionNguyen Hai
 
Commonly used design patterns
Commonly used design patternsCommonly used design patterns
Commonly used design patternsMojammel Haque
 
Beyond TDD: Enabling Your Team to Continuously Deliver Software
Beyond TDD: Enabling Your Team to Continuously Deliver SoftwareBeyond TDD: Enabling Your Team to Continuously Deliver Software
Beyond TDD: Enabling Your Team to Continuously Deliver SoftwareChris Weldon
 
Test in action – week 1
Test in action – week 1Test in action – week 1
Test in action – week 1Yi-Huan Chan
 

Similaire à Legacy Code Hands-on Session (20)

Sandro Mancuso – Testing and refactoring legacy code @ I T.A.K.E. Unconferenc...
Sandro Mancuso – Testing and refactoring legacy code @ I T.A.K.E. Unconferenc...Sandro Mancuso – Testing and refactoring legacy code @ I T.A.K.E. Unconferenc...
Sandro Mancuso – Testing and refactoring legacy code @ I T.A.K.E. Unconferenc...
 
Refactoring ASP.NET and beyond
Refactoring ASP.NET and beyondRefactoring ASP.NET and beyond
Refactoring ASP.NET and beyond
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agility
 
The Diabolical Developers Guide to Performance Tuning
The Diabolical Developers Guide to Performance TuningThe Diabolical Developers Guide to Performance Tuning
The Diabolical Developers Guide to Performance Tuning
 
2011-02-03 LA RubyConf Rails3 TDD Workshop
2011-02-03 LA RubyConf Rails3 TDD Workshop2011-02-03 LA RubyConf Rails3 TDD Workshop
2011-02-03 LA RubyConf Rails3 TDD Workshop
 
Bdd with m spec
Bdd with m specBdd with m spec
Bdd with m spec
 
The View - Lotusscript coding best practices
The View - Lotusscript coding best practicesThe View - Lotusscript coding best practices
The View - Lotusscript coding best practices
 
gDayX 2013 - Advanced AngularJS - Nicolas Embleton
gDayX 2013 - Advanced AngularJS - Nicolas EmbletongDayX 2013 - Advanced AngularJS - Nicolas Embleton
gDayX 2013 - Advanced AngularJS - Nicolas Embleton
 
Azure from scratch part 4
Azure from scratch part 4Azure from scratch part 4
Azure from scratch part 4
 
CNUG TDD June 2014
CNUG TDD June 2014CNUG TDD June 2014
CNUG TDD June 2014
 
Android antipatterns
Android antipatternsAndroid antipatterns
Android antipatterns
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha Touch
 
Groovy In the Cloud
Groovy In the CloudGroovy In the Cloud
Groovy In the Cloud
 
Test driven development v1.0
Test driven development v1.0Test driven development v1.0
Test driven development v1.0
 
Behavior Driven Development by Example
Behavior Driven Development by ExampleBehavior Driven Development by Example
Behavior Driven Development by Example
 
Test Driven Development Introduction
Test Driven Development IntroductionTest Driven Development Introduction
Test Driven Development Introduction
 
Agile
AgileAgile
Agile
 
Commonly used design patterns
Commonly used design patternsCommonly used design patterns
Commonly used design patterns
 
Beyond TDD: Enabling Your Team to Continuously Deliver Software
Beyond TDD: Enabling Your Team to Continuously Deliver SoftwareBeyond TDD: Enabling Your Team to Continuously Deliver Software
Beyond TDD: Enabling Your Team to Continuously Deliver Software
 
Test in action – week 1
Test in action – week 1Test in action – week 1
Test in action – week 1
 

Plus de Sandro Mancuso

Software Modernisation: a strategic approach
Software Modernisation: a strategic approachSoftware Modernisation: a strategic approach
Software Modernisation: a strategic approachSandro Mancuso
 
Aligning Product and Software Design
Aligning Product and Software DesignAligning Product and Software Design
Aligning Product and Software DesignSandro Mancuso
 
A Case for Outside-In Design
A Case for Outside-In DesignA Case for Outside-In Design
A Case for Outside-In DesignSandro Mancuso
 
Crafted Design - GeeCON 2014
Crafted Design - GeeCON 2014Crafted Design - GeeCON 2014
Crafted Design - GeeCON 2014Sandro Mancuso
 
Software Craftsmanship
Software CraftsmanshipSoftware Craftsmanship
Software CraftsmanshipSandro Mancuso
 
Software Craftsmanship - JAX London 2011
Software Craftsmanship - JAX London 2011Software Craftsmanship - JAX London 2011
Software Craftsmanship - JAX London 2011Sandro Mancuso
 

Plus de Sandro Mancuso (6)

Software Modernisation: a strategic approach
Software Modernisation: a strategic approachSoftware Modernisation: a strategic approach
Software Modernisation: a strategic approach
 
Aligning Product and Software Design
Aligning Product and Software DesignAligning Product and Software Design
Aligning Product and Software Design
 
A Case for Outside-In Design
A Case for Outside-In DesignA Case for Outside-In Design
A Case for Outside-In Design
 
Crafted Design - GeeCON 2014
Crafted Design - GeeCON 2014Crafted Design - GeeCON 2014
Crafted Design - GeeCON 2014
 
Software Craftsmanship
Software CraftsmanshipSoftware Craftsmanship
Software Craftsmanship
 
Software Craftsmanship - JAX London 2011
Software Craftsmanship - JAX London 2011Software Craftsmanship - JAX London 2011
Software Craftsmanship - JAX London 2011
 

Legacy Code Hands-on Session

  • 1. Testing and Refactoring Legacy Code Sandro Mancuso @sandromancuso craftedsw.blogspot.com
  • 2. What is this session about?
  • 3. Business Requirements Imagine a social networking website for travellers • You need to be logged in to see the content • You need to be a friend to see someone else’s trips
  • 4. Legacy Code Rules • You cannot change production code if not covered by tests – Just automated refactorings (via IDEs) are allowed, if needed to write the test
  • 5. Tips • Use a code coverage tool • Commit as often as possible – Enables you to commit (push) improvements quickly – Allows you to roll back (reset) if you get lost • When refactoring, try to stay in the green for as long as possible
  • 7. Working with Legacy Code Tips Start refactoring from deepest to shortest branch Start testing from shortest to deepest branch
  • 8. Trip Service - Problems <<Singleton>> UserSession +getInstance() : UserSession +getLoggedUser() : User TripService +findTripsByUser(User) : List<Trip> TripDAO +findTripsByUser(User) : List<Trip>
  • 9. Legacy Code Common Problems • Hard-wired dependencies – Create a “seam” to break dependencies • Extract dependency in a protected method • In our test, extend the class under test and override the method with the dependency so you can mock the result • Identify feature envy – Move behaviour out to the appropriate class
  • 11. Remember Start refactoring from deepest to shortest branch Start testing from shortest to deepest branch
  • 12. Refactoring vs Re-design • After localised refactoring, consider fixing the design • Writing tests for existing code may perpetuate a bad design
  • 13. Craftsmen at work • Write readable and maintainable code – Code must express business rules • Strive for simplicity • Know your tools well (i.e. frameworks, shortcuts) • Work in small and safe increments – Commit often • Embrace changes, be brave • Boy scout rule / No broken windows
  • 14. Thanks https://github.com/sandromancuso/trip-service-kata Sandro Mancuso @sandromancuso craftedsw.blogspot.com