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 Effectively with Legacy Code

5,961 views

Published on

Book review: Working Effectively with Legacy Code, by Michael C. Feathers

Agenda
- The mechanics of change
- Changing software
- Dependency breaking techniques

This session is for developers who need to work on code projects that where written without good unit-testing.

Published in: Technology, Education
  • Be the first to comment

Working Effectively with Legacy Code

  1. 1. 29 January 2010<br />Working Effectively with Legacy Code(book review)<br />
  2. 2. 29 January 2010<br />Working Effectively with Legacy Code (book review)<br />2<br />Bookreview<br />WorkingEffectivelywithLegacy Code<br />By Michael C. Feathers<br />http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052<br />Agenda<br />The mechanics of change<br />Changing software<br />Dependencybreakingtechniques<br />Thissession is fordeveloperswhoneed to workon code projectsthatwherewritten without goodunit-testing.<br />
  3. 3. Part 1: The mechanics of change<br />Workingeffectivelywithlegacy code<br />29 January 2010<br />3<br />Working Effectively with Legacy Code (book review)<br />
  4. 4. 4<br />Working Effectively with Legacy Code (book review)<br />29 January 2010<br />Four reasons to change software:<br />Adding a feature<br />Fixing a bug<br />Refactoring<br />Optimizing resource usage<br />Changing Software<br />
  5. 5. Four reasons to change software<br />29 January 2010<br />5<br />Working Effectively with Legacy Code (book review)<br />
  6. 6. Changing Software<br />Four reasons to change software:<br />Adding a feature<br />Fixing a bug<br />Refactoring<br />Optimizing resource usage<br />Risky change<br />What changes do we have to make?<br />How will we know that we’ve done them correctly?<br />How will we know that we haven’t broken anything?<br />29 January 2010<br />6<br />Working Effectively with Legacy Code (book review)<br />
  7. 7. What? Create another method for that? ...<br />Am I ready to do it? I guess I have to ...<br />If it’s not broke, don’t fix it !!!<br />Fear<br />Risky change<br />29 January 2010<br />7<br />Working Effectively with Legacy Code (book review)<br />
  8. 8. Edit and pray<br /> (Industry standard)<br />Cover and modify<br />Working with Feedback<br />Working Effectively with Legacy Code (book review)<br />29 January 2010<br />8<br />
  9. 9. Edit and pray (Industrystandard)<br />Carefully plan the changes<br />Make sure you understand the code<br />Start making changes<br />Poke around to see if you broke anything<br />Deploy to production environment<br />Wait for feedback from customers or elmah<br />29 January 2010<br />9<br />Working Effectively with Legacy Code (book review)<br />
  10. 10. Cover and modify<br />Safety net<br />Covering = testing<br />What is Unit Testing?<br />Higher level testing<br />Test coverings<br />The legacy code change algorithm<br />29 January 2010<br />10<br />Working Effectively with Legacy Code (book review)<br />
  11. 11. What is Unit testing?<br />Properties:<br />isFast = true;<br />canLocaliseProblems = true;<br />canTalkToDatabase = false;<br />canCommunicateWithNetwork = false;<br />canCommunicateWithLocalResources = false;<br />needsConfigChanges = false;<br />29 January 2010<br />11<br />Working Effectively with Legacy Code (book review)<br />
  12. 12. When we change code, we should have tests in place. <br />To put tests in place we need to change code<br />The legacy code dilemma<br />29 January 2010<br />12<br />Working Effectively with Legacy Code (book review)<br />
  13. 13. The legacy code change algorithm<br /><ul><li>Identify change points
  14. 14. Find test points
  15. 15. Break dependencies
  16. 16. Write tests
  17. 17. Make changes and refactor</li></ul>29 January 2010<br />13<br />Working Effectively with Legacy Code (book review)<br />
  18. 18. Sensing: we break dependencies to sensewhen we can’taccessvaluesour code computes.<br />Seperation: we break dependencies to seperatewhen we can’t even get a piece of code into a test harness to run.<br />Sensing and Seperation<br />29 January 2010<br />14<br />Working Effectively with Legacy Code (book review)<br />
  19. 19. Create a dummy or fake class<br />Add some methodssoyou can access the values to check<br />Repeat until all dependencies are tested<br />OR use a framework that allows us to do this<br />Faking collaborators<br />29 January 2010<br />15<br />Working Effectively with Legacy Code (book review)<br />
  20. 20. The Seam Model<br />29 January 2010<br />16<br />Working Effectively with Legacy Code (book review)<br />
  21. 21. Tools<br />29 January 2010<br />17<br />Working Effectively with Legacy Code (book review)<br />
  22. 22. Part 2: Changing Software<br />Working effectivelywith legacy code<br />29 January 2010<br />18<br />Working Effectively with Legacy Code (book review)<br />
  23. 23. Quick overview (what is yet to come)<br />I don’t have much time and I have to change it<br />It takes forever to make a change<br />How do I add a feature<br />I can’t get this class into a test harness<br />I can’t run this method in a test harness<br />I need to make a change. What methods should I test?<br />I need to make many changes in one area<br />I need to make a change, but I don’t know what tests to write<br />Dependencies on libraries are killing me<br />My application is all API calls<br />I don’t understand the code well enough to change it<br />This class is too big and I don’t want it to get any bigger.<br />I’m changing the same code all over the place<br />I need to change a monster method and I can’t write tests for it.<br />How do I know I’m not breaking anything?<br />We feel overwhelmed. It isn’t going to get any better.<br />29 January 2010<br />19<br />Working Effectively with Legacy Code (book review)<br />
  24. 24. I don’t have much time and I have to change it<br /><ul><li>Sprout method
  25. 25. Sprout class
  26. 26. Wrap method
  27. 27. Wrap class</li></ul>29 January 2010<br />20<br />Working Effectively with Legacy Code (book review)<br />
  28. 28. It takes forever to make a change<br /><ul><li>Understanding
  29. 29. Lag time
  30. 30. Breaking dependencies</li></ul>29 January 2010<br />21<br />Working Effectively with Legacy Code (book review)<br />
  31. 31. How do I add a feature<br />Test-Driven Development (TDD)<br />1. Write a failing test<br />2. Get it to compile<br />3. Make it pass <br />4. Remove duplication <br />5. Repeat <br />Programming by difference<br />29 January 2010<br />22<br />Working Effectively with Legacy Code (book review)<br />
  32. 32. How do I add a feature<br />Test-Driven Development (TDD) for legacy<br />0. Get the class you want to change under test<br />1. Write a failing test<br />2. Get it to compile<br />3. Make it pass (try not to change existing code)<br />4. Remove duplication <br />5. Repeat <br />Programming by difference<br />29 January 2010<br />23<br />Working Effectively with Legacy Code (book review)<br />
  33. 33. I can’t get this class into a test harness<br />The case of the ...<br /><ul><li>Irritating parameter
  34. 34. Hidden dependency
  35. 35. Construction blob
  36. 36. Irritating global dependencies
  37. 37. Onion parameter</li></ul>29 January 2010<br />24<br />Working Effectively with Legacy Code (book review)<br />
  38. 38. I can’t run this method in a test harness<br />The case of the ...<br /><ul><li>Hidden method
  39. 39. Undetectable side effect</li></ul>29 January 2010<br />25<br />Working Effectively with Legacy Code (book review)<br />
  40. 40. Reasoning about effects<br />Reasoning forward<br />Effect propagation<br />Tools for effect reasoning<br />I need to make a change. What methods should I test?<br />29 January 2010<br />26<br />Working Effectively with Legacy Code (book review)<br />
  41. 41. Interception points<br />Judging design with pinch points<br />Pinch point traps<br />I need to make many changes in one area<br />29 January 2010<br />27<br />Working Effectively with Legacy Code (book review)<br />
  42. 42. Characterization tests<br />Characterizing classes<br />Targeted testing<br />I need to make a change, but I don’t know what tests to write<br />29 January 2010<br />28<br />Working Effectively with Legacy Code (book review)<br />
  43. 43. Dependencies on libraries are killing me<br />29 January 2010<br />29<br />Working Effectively with Legacy Code (book review)<br />
  44. 44. I don’t understand the code well enough to change it<br /><ul><li>Notes/Sketching
  45. 45. Listing markup
  46. 46. Scratch refactoring
  47. 47. Delete unused code</li></ul>29 January 2010<br />30<br />Working Effectively with Legacy Code (book review)<br />
  48. 48. This class is too big and I don’t want it to get any bigger.<br /><ul><li>Seeing responsibilities
  49. 49. Other techniques</li></ul>29 January 2010<br />31<br />Working Effectively with Legacy Code (book review)<br />
  50. 50. I’m changing the same code all over the place<br />29 January 2010<br />32<br />Working Effectively with Legacy Code (book review)<br />
  51. 51. Varieties of monsters<br />Tackling monsters with automated refactoring support<br />Strategy<br />I need to change a monster method and I can’t write tests for it.<br />29 January 2010<br />33<br />Working Effectively with Legacy Code (book review)<br />
  52. 52. How do I know I’m not breaking anything?<br /><ul><li>Hyperaware editing
  53. 53. Single-goal editing
  54. 54. Preserve signatures
  55. 55. Lean on the compiler</li></ul>29 January 2010<br />34<br />Working Effectively with Legacy Code (book review)<br />
  56. 56. Create oases<br />We feel overwhelmed. It isn’t going to get any better.<br />29 January 2010<br />35<br />Working Effectively with Legacy Code (book review)<br />
  57. 57. 36<br />Working Effectively with Legacy Code (book review)<br />29 January 2010<br />

×