KEMBAR78
Legacy Code Hands-on Session | PPTX
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

Legacy Code Hands-on Session

  • 1.
    Testing and Refactoring Legacy Code Sandro Mancuso @sandromancuso craftedsw.blogspot.com
  • 2.
    What is thissession about?
  • 3.
    Business Requirements Imagine asocial 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 acode 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
  • 6.
  • 7.
    Working with LegacyCode 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 CommonProblems • 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
  • 10.
  • 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