code refactoring - live coding demo (javaday 2014)
DESCRIPTION
TRANSCRIPT
![Page 1: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/1.jpg)
Code RefactoringLive Coding Demo
Peter Kofler, ‘Code Cop’@codecopkofler
www.code-cop.org
Copyright Peter Kofler, licensed under CC-BY.
![Page 2: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/2.jpg)
Peter Kofler
• Ph.D. (Appl. Math.)
• Professional SoftwareDeveloper for 15 years
• Specialised in Re-Engineering of Legacy Code
• “fanatic about code quality”
![Page 3: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/3.jpg)
Trainingon the
Job?
![Page 4: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/4.jpg)
Yes, some but...
• only what is already there• Trial & Error not popular in production• no practice - only production• time pressure
![Page 5: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/5.jpg)
I help development teams with
• Professionalism
• Quality and Productivity
• Continuous Improvement
![Page 6: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/6.jpg)
Mentoring
• Pair Programming
• Programming Workshops
• Deliberate Practice, e.g. Coding Dojos
![Page 7: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/7.jpg)
Developing Quality Software Developers
![Page 8: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/8.jpg)
Refactoring is a technique for restructuring
an existing body of code, altering its internal structure
without changing its external behaviour.
(Martin Fowler)
![Page 9: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/9.jpg)
Welcome to the Gilded Rose
![Page 10: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/10.jpg)
The existing inventory system
• We have items to sell. Items degrade in quality the older they get.
• All items have a SellIn value which denotes the number of days we have to sell the item.
• All items have a Quality value which denotes how valuable the item is.
![Page 11: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/11.jpg)
Requirements
• At the end of each day our system lowers both values for every item.
• Once the sell by date has passed, Quality degrades twice as fast.
• The Quality of an item is never negative.• The Quality of an item is never more than 50.
![Page 12: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/12.jpg)
Special Item: Brie
• Aged Brie actually increases in Quality the older it gets.
![Page 13: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/13.jpg)
Backstage Passes
• Backstage passes, increases in Quality as it's SellIn value approaches (by a complex formula)
• but Quality drops to 0 after the concert.
![Page 14: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/14.jpg)
Special Item
• Sulfuras, a legendary item, never has to be sold or decreases in Quality.
![Page 15: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/15.jpg)
New Requirement
• We have recently signed a supplier of conjured items. This requires an update to our system:–Conjured items degrade in Quality twice as
fast as normal items.
![Page 16: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/16.jpg)
What Will We Do Now?● I will implement the new requirement.● We will change the code together.● I will (need) to refactor the existing code.
![Page 17: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/17.jpg)
Demo
Step by step: http://hg.code-cop.org/gilded-rose-refactoring/branch/refactor-7
![Page 18: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/18.jpg)
![Page 19: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/19.jpg)
![Page 20: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/20.jpg)
![Page 21: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/21.jpg)
Now the New Requirement!
• We have recently signed a supplier of conjured items. This requires an update to our system:–Conjured items degrade in Quality twice as
fast as normal items.
![Page 22: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/22.jpg)
What I did
• could not implement the requirement easily, had to refactor first.
• made it more readable.
• removed duplication (extracted duplicates).
• split method into logically coherent blocks.
![Page 23: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/23.jpg)
What I did
• simplified complex boolean conditions.
• replaced duplicated if-statements with polymorphy (extracted Strategy pattern).
• moved common code into the strategy.
• finally implemented the new requirement ;-)
![Page 24: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/24.jpg)
Read these books now!Explore the Refactorings!Practice them in your IDE!
![Page 25: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/25.jpg)
Peter [email protected]
Gilded Rose Kata byEmily Bache
@emilybachehttp://coding-is-like-cooking.info/2013/03/writing-good-tests-for-the-gilded-rose-kata/
![Page 26: Code Refactoring - Live Coding Demo (JavaDay 2014)](https://reader034.vdocument.in/reader034/viewer/2022051313/5495b930b47959f5708b4737/html5/thumbnails/26.jpg)
CC Images● Hamster http://www.flickr.com/photos/zebrapares/4529836138● master http://www.flickr.com/photos/sherpas428/4350620602/ ● pairing http://www.flickr.com/photos/dav/94735395/● inn http://www.flickr.com/photos/danielleblue/170496395/● Brie http://www.flickr.com/photos/chez_loulou/2767503201● pass http://www.flickr.com/photos/frf_kmeron/5556518514● Sulfuras https://www.flickr.com/photos/sharelabs/11195626116