Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Working with Legacy Code

4,192 views

Published on

A Presentation I did at Agile Practitioners 2015
http://apilconf.com/

Published in: Software

Working with Legacy Code

  1. 1. Here’s the codebase
  2. 2. About Me • Clean coder, TDDer, Software Craftsman • Over 12 years of development • Lead the infrastructure operations and development workflows at StartApp • Tech lead and scrum master (eBay) egolan74@gmail.com http://eyalgo.com/ @eyalgo_egolan
  3. 3. OOD Open mind Attitude over technique About the Presentation
  4. 4. What is Legacy Code? “…Legacy code refers to an application system source code that is no longer supported…” (techopedia)
  5. 5. This is Reality New features… Modify existing behavior… Removing features… Fixing bugs…
  6. 6. Working With Legacy Code Modify behavior of existing (unfamiliar) code
  7. 7. Identify requirements Understand the code Modify the code
  8. 8. I changed the ‘if’ with a ‘switch’. Is there a new bug?
  9. 9. Why Tests? Risky change Refactoring, new feature, bug Feedback Assurance and confidence
  10. 10. Summing Up We need to modify code We want safety using tests
  11. 11. Summing Up We need to modify code We want safety using tests The code does not have tests
  12. 12. What is Legacy Code ? (revised) “Code without tests” Michael Feathers (Working Effectively with Legacy Code)
  13. 13. What is Legacy Code ? (revised) “Code without tests” Michael Feathers (Working Effectively with Legacy Code) Untestable code
  14. 14. Process /pro-cess/ def: A number of actions or functions done in order to produce a desired result.
  15. 15. Identify requirements Understand the code Write tests Modify the code
  16. 16. Identify requirements Understand the code Write tests Modify the code Working with Legacy Code
  17. 17. Working With Legacy Code (revised)
  18. 18. Obstacles I don’t understand it I don’t have time Unstructured code Breaking dependencies
  19. 19. I Don’t Understand the Code Debugging…
  20. 20. I Don’t Understand the Code
  21. 21. I Don’t Understand the Code Extract Methods Move fields Create classes Throw that code away…
  22. 22. I Don’t Understand the Code
  23. 23. Difficult to Instantiate the Class
  24. 24. Difficult to Instantiate the Class Expose Static Method (which does not use instance’s variables) 1. Write test that access the method to be tested 2. Extract the body to a static method 3. Call the static method from the original one
  25. 25. Adapt Complicated Class Adapter Test with fake
  26. 26. Object Creation in the Constructor
  27. 27. Object Creation in the Constructor In your test: create a subclass for testing
  28. 28. Static Methods Test this Fake this
  29. 29. Static Methods Create a wrapping instance method Fake this
  30. 30. Singleton
  31. 31. Singleton Introduce a setter for ‘instance’
  32. 32. Factory Method
  33. 33. Factory Method 1 Inject fake server
  34. 34. Hidden Method • Test through a public method • Extract to a new class • Expose the method
  35. 35. Did I Break Something? Do one thing at a time Preserve signature Trust your computer and IDE Pair programming
  36. 36. Huge Method • Usually not following SRP • Usually with too many dependencies • Invest time and create a safety harness • Refactor: extract to smaller, tested methods, extract class
  37. 37. More Operations Introduce / Extract interface Rename (better understanding) Expose visibility (package protected) Extract and override
  38. 38. Tools Use your IDE mockito PowerMock and EasyMock Pair Programming
  39. 39. Preliminary Knowledge OOD SOLID Unit testing frameworks Clean code Refactoring principles
  40. 40. Attitude
  41. 41. Ask Yourself 1. What changes do we have to make? 2. How will we know that we’ve done them correctly? 3. How will we know we haven’t broken anything?
  42. 42. I Don’t Have Time
  43. 43. Always leave the place cleaner than you found it Boy scouts Rule
  44. 44. Q & A Thank You egolan74@gmail.com

×