Slide 1

Slide 1 text

www .codingzeal.com @codingzeal Randy Coulman Getting a Handle on Legacy Code Senior Software Engineer @randycoulman

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

The System

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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.

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Legacy Code How do we safely change legacy code?

Slide 13

Slide 13 text

The Legacy Code Algorithm

Slide 14

Slide 14 text

The Legacy Code Algorithm 1. Identify change points

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

The Legacy Code Algorithm 1. Identify change points 2. Find test points 3. Break dependencies 4. Write tests 5. Make changes and refactor

Slide 19

Slide 19 text

The Legacy Code Algorithm https://twitter.com/mfeathers/status/589118880825495552

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

The Code

Slide 22

Slide 22 text

Mob Programming Exercise Write characterization tests

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Mob Programming Exercise Use Approvals to simplify testing

Slide 25

Slide 25 text

Cleaning Up the Code Fight the urge to rewrite

Slide 26

Slide 26 text

Cleaning Up the Code The Boy Scout Rule: "Leave the campground cleaner than you found it"

Slide 27

Slide 27 text

Cleaning Up the Code Baby Steps Lots of tiny, safe refactorings add up to big changes

Slide 28

Slide 28 text

Cleaning Up the Code Don’t Boil the Ocean Stay focused

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Mob Programming Exercise Refactor for better test points

Slide 32

Slide 32 text

Small Group Exercise Write a few more-targeted tests

Slide 33

Slide 33 text

Small Group Exercise Refactor enough to add new feature

Slide 34

Slide 34 text

Mob Programming Exercise More refactoring

Slide 35

Slide 35 text

Public Service Announcement

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

References Approvals https://github.com/kytrinyx/approvals SimpleCov https://github.com/colszowka/simplecov Flog https://github.com/seattlerb/flog

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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