Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

Legacy Code Hands-on Session

1.616 visualizaciones

Publicado el

Complementary slide deck for my hands-on coding session on testing and refactoring legacy code.

Code can be found here: https://github.com/sandromancuso/trip-service-kata

  • Sé el primero en comentar

Legacy Code Hands-on Session

  1. 1. Testing and Refactoring Legacy Code Sandro Mancuso @sandromancuso craftedsw.blogspot.com
  2. 2. What is this session about?
  3. 3. Business RequirementsImagine a social networking website fortravellers• You need to be logged in to see the content• You need to be a friend to see someone else’s trips
  4. 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. 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
  6. 6. Exercise
  7. 7. Working with Legacy Code Tips Start refactoring from deepest to shortest branchStart testing fromshortest todeepest branch
  8. 8. Trip Service - Problems <<Singleton>> UserSession +getInstance() : UserSession +getLoggedUser() : UserTripService+findTripsByUser(User) : List<Trip> TripDAO +findTripsByUser(User) : List<Trip>
  9. 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
  10. 10. Exercise
  11. 11. Remember Start refactoring from deepest to shortest branchStart testing fromshortest todeepest branch
  12. 12. Refactoring vs Re-design• After localised refactoring, consider fixing the design• Writing tests for existing code may perpetuate a bad design
  13. 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. 14. Thankshttps://github.com/sandromancuso/trip-service-kata Sandro Mancuso @sandromancuso craftedsw.blogspot.com

×