Getting a Handle on Legacy Code

Getting a Handle on Legacy Code

We all run into legacy code. Sometimes, we even write it ourselves. Working with legacy code can be a daunting challenge, but there are ways to tackle it without taking on the risk of a full rewrite.

There is a deep satisfaction that comes from conquering a nasty piece of legacy code. The ability to achieve this goal depends on both testing and refactoring.

We'll learn how baby-step refactoring techniques lead to a better understanding of the code and a high-quality design while always keeping the code running.

This is a workshop, so a lot of the material was hands-on practice and not captured in these slides.

27204e228cc521c6cafed3c92b95184c?s=128

Randy Coulman

April 22, 2015
Tweet

Transcript

  1. www .codingzeal.com @codingzeal Randy Coulman Getting a Handle on Legacy

    Code Senior Software Engineer @randycoulman
  2. Welcome to our new job! Enhance the inventory management system

    for the Gilded Rose Inn
  3. The System

  4. The System • Items have a sell_in value: the number

    of days we have to sell the item
  5. The System • Items have a sell_in value: the number

    of days we have to sell the item • Items have a quality value: how valuable the item is
  6. The System • Items have a sell_in value: the number

    of days we have to sell the item • Items have a quality value: how valuable the item is • At the end of each day our system lowers both values for every item
  7. The Spec: Can We Trust It? • Once the sell-by

    date has passed, quality degrades twice as fast • The quality of an item is never negative • “Aged Brie" actually increases in quality the older it gets • The quality of an item is never more than 50 • "Sulfuras", being a legendary item, never has to be sold or decreases in quality • "Backstage passes", like aged brie, increases in quality as its sell-by date approaches; quality increases by 2 when there are 10 days or less and by 3 when there are 5 days or less, but quality drops to 0 after the concert.
  8. Constraint The Item class and @items property are off-limits No

    collective code ownership
  9. Our Task: Support Conjured items Conjured items degrade in quality

    twice as fast as normal items
  10. Workshop Structure Mix of: • Presentation/demo • Mob programming •

    Small group/pair/individual work
  11. Mob Programming • One computer for programming • One driver

    • N navigators and researchers http://mobprogramming.org/mob-programming-basics/
  12. Legacy Code How do we safely change legacy code?

  13. The Legacy Code Algorithm

  14. The Legacy Code Algorithm 1. Identify change points

  15. The Legacy Code Algorithm 1. Identify change points 2. Find

    test points
  16. The Legacy Code Algorithm 1. Identify change points 2. Find

    test points 3. Break dependencies
  17. The Legacy Code Algorithm 1. Identify change points 2. Find

    test points 3. Break dependencies 4. Write tests
  18. The Legacy Code Algorithm 1. Identify change points 2. Find

    test points 3. Break dependencies 4. Write tests 5. Make changes and refactor
  19. The Legacy Code Algorithm https://twitter.com/mfeathers/status/589118880825495552

  20. Code For the Workshop https://github.com/CodingZeal/gilded-rose-railsconf2015

  21. The Code

  22. Mob Programming Exercise Write characterization tests

  23. Golden Master Testing a.k.a. Guru Checks Output anti-pattern Katrina Owen’s

    Approvals Gem
  24. Mob Programming Exercise Use Approvals to simplify testing

  25. Cleaning Up the Code Fight the urge to rewrite

  26. Cleaning Up the Code The Boy Scout Rule: "Leave the

    campground cleaner than you found it"
  27. Cleaning Up the Code Baby Steps Lots of tiny, safe

    refactorings add up to big changes
  28. Cleaning Up the Code Don’t Boil the Ocean Stay focused

  29. Cleaning Up the Code https://twitter.com/KentBeck/status/250733358307500032

  30. Cleaning Up the Code https://twitter.com/supermoof/status/478322429257715712

  31. Mob Programming Exercise Refactor for better test points

  32. Small Group Exercise Write a few more-targeted tests

  33. Small Group Exercise Refactor enough to add new feature

  34. Mob Programming Exercise More refactoring

  35. Public Service Announcement

  36. Want More? Original Gilded Rose Kata http://craftsmanship.sv.cmu.edu/katas/gilded-rose-kata Gilding the Rose

    Talk at GoGaRuCo 2014 http://confreaks.tv/videos/gogaruco2014-gilding-the-rose-refactoring-legacy-code Code from GoGaRuCo 2014 Talk https://github.com/randycoulman/GildedRose
  37. References Working Effectively With Legacy Code: Michael Feathers http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052 Refactoring:

    Martin Fowler http://www.amazon.com/Refactoring-Improving-Design-Existing-Code/dp/0201485672
  38. References Approvals https://github.com/kytrinyx/approvals SimpleCov https://github.com/colszowka/simplecov Flog https://github.com/seattlerb/flog

  39. Questions ? http://randycoulman.com http://speakerdeck.com/randycoulman http://speakerrate.com/randycoulman Code is on Github https://github.com/CodingZeal/gilded-rose-railsconf2015

  40. www .codingzeal.com @codingzeal Thank You! Randy Coulman @randycoulman