Taming scary production code
that nobody wants to touch
Mike Clement
Founding Software Craftsman at Greater Sum
mike@softwareontheside.com
http://blog.softwareontheside.com
http://www.greatersum.com
Modernizing Legacy Code
Mike Clement
Founding Software Craftsman at Greater Sum
mike@softwareontheside.com
http://blog.softwareontheside.com
http://www.greatersum.com
What is Legacy Code?
Legacy code is code that we’ve gotten
from someone else.
Legacy code is code that we’ve gotten
from past-self.
A slang term for difficult-to-change code
that we don’t understand.
Legacy code is simply code without tests.
Code without tests is bad code.
Code without tests is bad code?
Code without tests is scary code.
Legacy code (code without tests) is scary
production code that nobody wants to
touch.
Photo by Yung Chang on Unsplash
Risk
Photo by Leio McLaren on Unsplash
To mitigate risk, we have to ask three
questions:
1. What changes do we have to make?
2. How will we know that we’ve done them correctly?
3. How will we know that we haven’t broken anything?
How much change can you afford if
changes are risky?
Is it more important for the software
system to work
OR
is it more important for the software
system to be easy to change?
“If you give me a program that does not work
but is easy to change, then I can make it work,
and keep it working as requirements change.
Therefore the program will remain continually
useful.”
Robert C. Martin
How do we keep software soft?
Legacy/Scary Code Dilemma
When we change code, we should have
tests in place.
To put tests in place, we often have to
change code.
Legacy Code Dilemma
When we change code, we should have
tests in place.
To put tests in place, we often have to
change code.
Low Risk Changes via
Core Refactorings
re·fac·tor·ing (noun):
a change made to the internal structure of
software… without changing its observable
behavior.
https://martinfowler.com/bliki/DefinitionOfRefactoring.html
Core Refactorings as CRUD
• Create: Introduce/Extract:
• Local Variable
• Method
• Parameter
• Field
• Read: (performed by the human; no refactoring needed)
• Update: Rename
• Delete: Inline:
• Local Variable
• Method
• Parameter
• Field
http://arlobelshee.com/the-core-6-refactorings/
Core Refactorings Demo
Rental Store Code
To mitigate risk, we have to ask three
questions:
1. What changes do we have to make?
2. How will we know that we’ve done them correctly?
3. How will we know that we haven’t broken anything?
Use your tools!
Preserving existing behavior is one of the
largest challenges in software development.
Photo by Ben Koorengevel on Unsplash
Edit and Pray
Photo by Ian Espinosa on Unsplash
“Working with care”
Cover and Modify
Preserving existing behavior is one of the
largest challenges in software development.
Characterization Tests
char·ac·ter·i·za·tion (noun)
a description of the distinctive nature or
features of something.
Characterization tests
a means to protect existing behavior of legacy code
against unintended changes
Characterization tests
a means to describe (characterize) the actual
behavior of an existing piece of software
Photo by Ksenia Makagonova on Unsplash
Do not check for “correctness”
Check for existing behavior
Don’t have to understand
ApprovalTests Show & Tell
An open source assertion/verification library to aid unit testing
Seams
A seam is a place where you can alter
behavior in your program without editing
in that place.
Method boundary
“Peel” Demo
Inject your dependency
“Slice” Show & Tell
https://twitter.com/kentbeck/status/250733358307500032?lang=en
Resources
• Working Effectively with Legacy Code by Michael Feathers
• Clean Architecture by Robert C. Martin
• Peel & Slice videos by Llewellyn Falco
• 6 Core Refactorings by Arlo Belshee
Mike Clement
• @mdclement
• mike@softwareontheside.com
• http://blog.softwareontheside.com
• https://github.com/mdclement
• Greater Sum
• @thegreatersum
• http://www.greatersum.com
• Software Craftsmanship Atlanta
• Find us on meetup.com
• Limited WIP Society Atlanta
• Find us on meetup.com

Taming scary production code that nobody wants to touch

Editor's Notes

  • #22 The Legacy Code Dilemma
  • #23 The Legacy Code Dilemma
  • #34 then you poke around further to make sure that you didn’t break anything. The poking around is essential. When you make your changes, you are hoping and praying that you’ll get them right, and you take extra time when you are done to make sure that you did.
  • #46 When we have tests that detect change, it is like having a vise around our code.
  • #54 Peel off the dependency
  • #59 for each desired change, make the change easy (warming: this may be hard), then make the easy change Kent Beck