Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Refactoring When You Literally Can't Even

Refactoring When You Literally Can't Even

Refactoring is the process of changing the structure of a codebase without changing its external behavior. In addition to being an important part of ongoing development, it can also be used to get a legacy codebase under control.

Using the Gilded Rose Kata as a basis, this workshop explores how to refactor a legacy codebase safely, even when you don't yet understand what the code is doing.


Randy Coulman

May 12, 2016


  1. Workshop Setup Navigate to https://github.com/CodingZeal/gilded-rose-railsconf2015 Click on the Nitrous QuickStart

    button Create a new account or Log in to your existing account
  2. www .codingzeal.com @codingzeal Randy Coulman a.k.a. Refactron Refactoring When You

    Literally Can’t Even Senior Software Engineer @randycoulman
  3. Welcome to our new job! Enhance the inventory management system

    for the Gilded Rose Inn
  4. The System • Sell_in: the number of days we have

    to sell the item • Quality: how valuable the item is • End of each day: lower both values for every item
  5. 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.
  6. Constraint The Item class and @items property are off-limits No

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

    twice as fast as normal items
  8. Workshop Structure Mix of: • Presentation • Mob programming •

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

    • N navigators and researchers http://mobprogramming.org/mob-programming-basics/
  10. How to Change Code https://twitter.com/KentBeck/status/250733358307500032

  11. What is Refactoring? “a disciplined technique for restructuring an existing

    body of code, altering its internal structure without changing its external behavior” http://refactoring.com
  12. Refactoring

  13. Refactoring Many refactorings are mechanical

  14. When to Refactor? Get legacy code under control

  15. When to Refactor? An ongoing part of your workflow Test-Driven

    Development (TDD) Cycle: Red -> Green -> Refactor
  16. Why Refactor? Make the code reflect your current understanding of

    the system
  17. Why Refactor? Ideal: Make the code look like it would

    have looked if the feature you just added had always been there
  18. Refactoring Requires test coverage

  19. Refactoring Legacy Code The Scouting Rule: "Leave the campground cleaner

    than you found it"
  20. Refactoring Legacy Code Baby Steps Lots of tiny, safe refactorings

    add up to big changes
  21. Refactoring Legacy Code Don’t Boil the Ocean Stay focused on

    your task
  22. Refactoring Legacy Code https://twitter.com/supermoof/status/478322429257715712

  23. The Code

  24. 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
  25. 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 Refactoring Ruby Edition: Jay Fields, Shane Harvie, and Martin Fowler http://www.amazon.com/Refactoring-Ruby-Jay-Fields/dp/0321603508
  26. Questions ? http://randycoulman.com Code is on Github https://github.com/CodingZeal/gilded-rose-railsconf2015 Slides Are

    on Speakerdeck https://speakerdeck.com/randycoulman
  27. www .codingzeal.com @codingzeal Thank You! Randy Coulman @randycoulman