Confident Refactors
View Slide
Mehdi Lahmam@mehlah
25kVRails app, started in Feb 2009Never rewritten23k+ commits from 52 contributors
Ruby is a massivelysuccessful language!
Early successMaking it easy to make new things
Later successMaking it easy to maintain old things
Today, let’s refactorsome legacy code
Refactor - verbTo change the design of codewithout changing its observable behavior.
Refactor - verbTo change in advance of a new featureor bug fix, making the job easier.
Legacy - nounOld code.
Legacy - nounCode without tests.
Legacy - nounCode that we don’t like.
Legacy - nounCode we don’t understand well enough to change confidently.
Refactoring is hard
Refactoring legacy codeis very hard
Easy to accidentallybreak functionality
Legacy refactorsfeels unsafe
Legacy refactorsare hard to sell
Business priorityCost / risks
Business priorityCost / risksNew features
Business priorityCost / risksNew featuresBugs fixes
Business priorityCost / risksNew featuresBugs fixesTesting
Business priorityCost / risksNew featuresBug fixesTesting Refactoring
Business priorityCost / risksNew featuresBug fixesTesting Refactoringno sellingneededeasy to sellcan often sellvery hard to sell
Selling refactoring is hard⏲ ⛔
Business priorityCost / risksRefactoring
Too much pressure ⏳ ⚒
Refactors are scary
What techniques we have?PDD - Pray Driven DevelopmentFowler’s refactoring bookCharacterization testingA/B testing, experiments
Refactors as surgeries
Refactors as surgeries Requires careful planningFollow a clear processMultiple observationsFlexible toolsThings can get bloody
1. Plan2. Cut3. Record4. Validate5. Refactor6. Verify7. Compare8. Fallback9. DeleteClear process
Cut
Record
Validate ✅
Refactor
Verify
Compare ⚖
Fallback
Delete ✂
We dit it
We dit it Time to celebrate hard