$30 off During Our Annual Pro Sale. View Details »

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
Tweet

More Decks by Randy Coulman

Other Decks in Programming

Transcript

  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

    View Slide

  2. www
    .codingzeal.com
    @codingzeal
    Randy Coulman
    a.k.a. Refactron
    Refactoring When You
    Literally Can’t Even
    Senior Software Engineer
    @randycoulman

    View Slide

  3. Welcome to our new job!
    Enhance the inventory management system
    for the Gilded Rose Inn

    View Slide

  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

    View Slide

  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.

    View Slide

  6. Constraint
    The Item class and @items property
    are off-limits
    No collective code ownership

    View Slide

  7. Our Task: Support Conjured items
    Conjured items degrade in quality
    twice as fast as normal items

    View Slide

  8. Workshop Structure
    Mix of:
    • Presentation
    • Mob programming
    • Small group/pair/individual work

    View Slide

  9. Mob Programming
    • One computer for programming
    • One driver
    • N navigators and researchers
    http://mobprogramming.org/mob-programming-basics/

    View Slide

  10. How to Change Code
    https://twitter.com/KentBeck/status/250733358307500032

    View Slide

  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

    View Slide

  12. Refactoring

    View Slide

  13. Refactoring
    Many refactorings are mechanical

    View Slide

  14. When to Refactor?
    Get legacy code under control

    View Slide

  15. When to Refactor?
    An ongoing part of your workflow
    Test-Driven Development (TDD) Cycle:
    Red -> Green -> Refactor

    View Slide

  16. Why Refactor?
    Make the code reflect your current
    understanding of the system

    View Slide

  17. Why Refactor?
    Ideal: Make the code look like it would have
    looked if the feature you just added had
    always been there

    View Slide

  18. Refactoring
    Requires test coverage

    View Slide

  19. Refactoring Legacy Code
    The Scouting Rule:
    "Leave the campground cleaner
    than you found it"

    View Slide

  20. Refactoring Legacy Code
    Baby Steps
    Lots of tiny, safe refactorings
    add up to big changes

    View Slide

  21. Refactoring Legacy Code
    Don’t Boil the Ocean
    Stay focused on your task

    View Slide

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

    View Slide

  23. The Code

    View Slide

  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

    View Slide

  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

    View Slide

  26. Questions ?
    http://randycoulman.com
    Code is on Github
    https://github.com/CodingZeal/gilded-rose-railsconf2015
    Slides Are on Speakerdeck
    https://speakerdeck.com/randycoulman

    View Slide

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

    View Slide