SlideShare a Scribd company logo
Working 
Effec-vely 
with 
Legacy 
Code 
Lessons 
in 
Prac-ce
The 
Book 
by 
Michael 
C. 
Feathers 
published 
2004 
foreword 
by 
“Uncle 
Bob” 
Mar-n 
influenced 
by 
Mar-n 
Fowler’s 
Refactoring
What 
is 
Legacy 
Code? 
• “difficult 
to 
change 
code 
that 
I 
don’t 
understand” 
• “code 
wriNen 
a 
long 
-me 
ago” 
• “code 
that 
somebody 
else 
wrote”
Why 
do 
I 
need 
to 
understand 
the 
code? 
because 
SoTware 
is 
Never 
Done 
Refactor 
Add 
Feature 
Fix 
Bug 
Op4mize 
Structure 
Changes 
Changes 
Changes 
New 
Behavior 
Changes 
Exis-ng 
Behavior 
Changes 
Resource 
Usage 
Changes
What 
is 
Legacy 
Code? 
“Code 
Without 
Tests”
Two 
Methods 
of 
SoTware 
Development 
“Edit 
and 
Pray” 
vs. 
“Cover 
and 
Modify” 
which 
brings 
us 
to…
The 
Legacy 
Code 
Dilemma 
“When 
we 
change 
code, 
we 
should 
have 
tests 
in 
place.” 
“To 
put 
tests 
in 
place, 
we 
oTen 
have 
to 
change 
code.” 
(But 
why?)
Legacy 
Code 
Change 
Algorithm 
1. Iden-fy 
Change 
Point 
2. Find 
Test 
Point 
3. Break 
Dependency 
4. Write 
Tests 
5. Change 
and 
Refactor
Seams 
Exchangeable 
Behavior 
+ 
an 
Enabling 
Point. 
Seams 
let 
you 
subs7tute 
one 
behavior 
for 
another 
by 
edi-ng 
only 
at 
the 
enabling 
point. 
Seams 
let 
you 
introduce 
test 
collaborators.
Object 
Seams 
Most 
useful 
type 
of 
seam 
for 
OOP. 
Another 
way 
of 
talking 
about 
polymorphism. 
Code 
without 
seam 
Code 
with 
seam
In 
the 
Wild 
No 
Tests 
== 
No 
Up-­‐to-­‐Date 
Documenta-on 
instead 
of
In 
the 
Wild 
Duplicate 
Code
In 
the 
Wild 
Long, 
Procedural 
Methods 
def generate_pdf 
end
In 
the 
Wild 
Stateful 
Programming 
• Keeping 
Track 
of 
Flags 
• HTTP 
Session 
Abuse
In 
the 
Wild 
Curse 
of 
MVC 
• Fat 
Models, 
Fat 
Controllers 
• Overloaded 
Responsibili-es
So 
Many 
Problems 
Chapter 
6: 
“I 
Don’t 
Have 
Much 
Time 
and 
I 
Have 
to 
Change 
It” 
Chapter 
8: 
“It 
Takes 
Forever 
to 
Make 
a 
Change” 
Chapter 
10: 
“I 
Can’t 
Run 
This 
Method 
in 
a 
Test” 
Chapter 
16: 
“I 
Don’t 
Understand 
the 
Code 
Well 
Enough 
To 
Change 
It” 
Chapter 
17: 
“My 
Applica-on 
Has 
No 
Structure” 
Chapter 
19: 
“My 
Project 
Is 
Not 
Object 
Oriented. 
How 
Do 
I 
Make 
Safe 
Changes? 
Chapter 
20: 
“This 
Class 
Is 
Too 
Big 
and 
I 
Don’t 
Want 
It 
to 
Get 
Any 
Bigger”
This 
Class 
is 
Too 
Big 
Methods 
defined 
on 
Job:
Single 
Responsibility 
Principle 
“Every 
class 
should 
have 
a 
single 
responsibility: 
It 
should 
have 
a 
single 
purpose 
in 
the 
system, 
and 
there 
should 
be 
only 
one 
reason 
to 
change 
it.”
Seeing 
Responsibili-es 
Group 
Methods
Seeing 
Responsibili-es 
Effect 
Sketch: 
Show 
Internal 
Rela-onships
Seeing 
Responsibili-es 
Primary 
Responsibility 
“Job 
presents 
damper 
inspec-on 
data 
in 
report 
form.” 
“Job 
generates 
files 
of 
reports.” 
“Job 
deletes 
report 
files.” 
“Job 
produces 
graphs 
of 
damper 
inspec-on 
data.” 
“Job 
persists 
and 
retrieves 
representa-ons 
of 
jobs 
in 
a 
database.”
Iden-fy 
Change 
Point 
We 
want 
to 
refactor 
Job 
to 
extract 
Reporter. 
1. Iden-fy 
Change 
Point 
2. Find 
Test 
Point 
3. Break 
Dependency 
4. Write 
Tests 
5. Change 
and 
Refactor
Find 
Test 
Point 
Intercep-on 
Points 
Higher 
Level 
Tes-ng 
– don’t 
have 
to 
break 
dependencies 
– verifies 
that 
your 
feature 
actually 
works 
– slow, 
inconvenient 
– hard 
to 
automate 
– not 
tes-ng 
in 
isola-on 
Progressive 
Strategy 
1. Iden-fy 
Change 
Point 
2. Find 
Test 
Point 
3. Break 
Dependency 
4. Write 
Tests 
5. Change 
and 
Refactor
Break 
Dependencies 
Required 
for 
unit 
tes-ng. 
Less 
necessary 
for 
higher-­‐level 
tes-ng. 
We’re 
gonna 
skip 
it 
(but 
just 
this 
-me). 
1. Iden-fy 
Change 
Point 
2. Find 
Test 
Point 
3. Break 
Dependency 
4. Write 
Tests 
5. Change 
and 
Refactor
Write 
Tests 
“Tes-ng” 
in 
the 
Interac-ve 
Console: 
1. Iden-fy 
Change 
Point 
2. Find 
Test 
Point 
3. Break 
Dependency 
4. Write 
Tests 
5. Change 
and 
Refactor
Change 
and 
Refactor 
This 
was 
harder 
than 
it 
looks. 
1. Iden-fy 
Change 
Point 
2. Find 
Test 
Point 
3. Break 
Dependency 
4. Write 
Tests 
5. Change 
and 
Refactor
Refactored 
Code 
Five 
different 
extracted 
classes 
(plus 
two 
more)
Extract 
Class…Again
BeNer 
With 
Seam? 
We 
can 
add 
a 
seam 
by 
injec-ng 
the 
dependency 
on 
the 
reporter. 
This 
way, 
we 
can 
test 
in 
isola-on 
by 
passing 
in 
a 
FakeReporter. 
Unfortunately, 
this 
makes 
our 
produc-on 
code 
ugly:
Seams 
Everywhere
Legacy 
Code 
Ain’t 
So 
Bad 
• No 
writer’s 
block. 
• Learn 
as 
you 
go. 
• Easy 
scapegoat! 
• Unavoidable, 
so 
learn 
to 
love 
it.
Thanks 
J 
Working 
Effec7vely 
with 
Legacy 
Code 
Michael 
Feathers 
Amar 
Shah 
@amar47shah
because 
SoTware 
is 
Never 
Done 
Refactor 
to 
Remove 
Demeter 
Viola-on

More Related Content

PPTX
Working Effectively with Legacy Code
PPTX
Working with Legacy Code
PDF
Working With Legacy Code
PPT
Getting Unstuck: Working with Legacy Code and Data
PDF
Unit testing legacy code
PDF
Working Effectively with Legacy Code
PPT
Working Effectively With Legacy Code
PDF
Adding Unit Test To Legacy Code
Working Effectively with Legacy Code
Working with Legacy Code
Working With Legacy Code
Getting Unstuck: Working with Legacy Code and Data
Unit testing legacy code
Working Effectively with Legacy Code
Working Effectively With Legacy Code
Adding Unit Test To Legacy Code

What's hot (20)

PPTX
Building unit tests correctly with visual studio 2013
PPTX
VT.NET 20160411: An Intro to Test Driven Development (TDD)
PDF
iOS Test-Driven Development
PDF
Refactoring Legacy Code
PDF
Unit Test + Functional Programming = Love
PDF
Unit Testing 101
PPTX
TDD - Test Driven Development
PPTX
TDD Basics with Angular.js and Jasmine
PPT
TDD And Refactoring
PDF
Unit Testing Best Practices
PDF
Unit testing (workshop)
PDF
Unit Testing Fundamentals
PPTX
Battle of The Mocking Frameworks
ODP
Embrace Unit Testing
PPTX
Unit testing - the hard parts
PPTX
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
PPTX
Principles and patterns for test driven development
PDF
Test Driven Development
PPTX
Roy Osherove TDD From Scratch
PPTX
Refactoring legacy code driven by tests - ENG
Building unit tests correctly with visual studio 2013
VT.NET 20160411: An Intro to Test Driven Development (TDD)
iOS Test-Driven Development
Refactoring Legacy Code
Unit Test + Functional Programming = Love
Unit Testing 101
TDD - Test Driven Development
TDD Basics with Angular.js and Jasmine
TDD And Refactoring
Unit Testing Best Practices
Unit testing (workshop)
Unit Testing Fundamentals
Battle of The Mocking Frameworks
Embrace Unit Testing
Unit testing - the hard parts
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Principles and patterns for test driven development
Test Driven Development
Roy Osherove TDD From Scratch
Refactoring legacy code driven by tests - ENG
Ad

Viewers also liked (6)

PDF
NewWork in der Praxis
PDF
Integration Test Hell
PDF
Mockist vs Classicists TDD
PDF
Quo vadis DevOps
PDF
Von Kutschern, Managern und Systemadministratoren
PDF
DevOps jenseits der Tools
NewWork in der Praxis
Integration Test Hell
Mockist vs Classicists TDD
Quo vadis DevOps
Von Kutschern, Managern und Systemadministratoren
DevOps jenseits der Tools
Ad

Similar to Working Effectively with Legacy Code: Lessons in Practice (20)

PDF
Workshop fight legacy code write unit test
PDF
[XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]
PPTX
Taming scary production code that nobody wants to touch
PPTX
Testing the untestable
PDF
Testing legacy code
PPTX
Refactoring workshop
PPTX
Working Effectively With Legacy Code
PPTX
Working with Legacy Code
PPTX
Entering the matrix
PDF
Legacy Coderetreat @Budapest 2013 02 16
PPTX
Escape the legacy code matrix - Vimercate
PDF
Let's test!
PPTX
Unit testing
PDF
Ddc2011 효과적으로레거시코드다루기
PDF
Summit 16: Stop Writing Legacy Code!
PPTX
An Introduction to Developer Testing
ZIP
Test Driven Development
PPTX
Bye Bye Cowboy Coder Days! (Legacy Code & TDD)
PPTX
Test Drive Dirven Driver HAHAahhaha.pptx
PPTX
Testing in Legacy: From Rags to Riches
Workshop fight legacy code write unit test
[XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]
Taming scary production code that nobody wants to touch
Testing the untestable
Testing legacy code
Refactoring workshop
Working Effectively With Legacy Code
Working with Legacy Code
Entering the matrix
Legacy Coderetreat @Budapest 2013 02 16
Escape the legacy code matrix - Vimercate
Let's test!
Unit testing
Ddc2011 효과적으로레거시코드다루기
Summit 16: Stop Writing Legacy Code!
An Introduction to Developer Testing
Test Driven Development
Bye Bye Cowboy Coder Days! (Legacy Code & TDD)
Test Drive Dirven Driver HAHAahhaha.pptx
Testing in Legacy: From Rags to Riches

Recently uploaded (20)

PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Nekopoi APK 2025 free lastest update
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
medical staffing services at VALiNTRY
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
L1 - Introduction to python Backend.pptx
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Introduction to Artificial Intelligence
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Navsoft: AI-Powered Business Solutions & Custom Software Development
How to Choose the Right IT Partner for Your Business in Malaysia
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Nekopoi APK 2025 free lastest update
Reimagine Home Health with the Power of Agentic AI​
Upgrade and Innovation Strategies for SAP ERP Customers
Computer Software and OS of computer science of grade 11.pptx
medical staffing services at VALiNTRY
Digital Systems & Binary Numbers (comprehensive )
PTS Company Brochure 2025 (1).pdf.......
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
L1 - Introduction to python Backend.pptx
Design an Analysis of Algorithms I-SECS-1021-03
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Introduction to Artificial Intelligence
Odoo POS Development Services by CandidRoot Solutions
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx

Working Effectively with Legacy Code: Lessons in Practice

  • 1. Working Effec-vely with Legacy Code Lessons in Prac-ce
  • 2. The Book by Michael C. Feathers published 2004 foreword by “Uncle Bob” Mar-n influenced by Mar-n Fowler’s Refactoring
  • 3. What is Legacy Code? • “difficult to change code that I don’t understand” • “code wriNen a long -me ago” • “code that somebody else wrote”
  • 4. Why do I need to understand the code? because SoTware is Never Done Refactor Add Feature Fix Bug Op4mize Structure Changes Changes Changes New Behavior Changes Exis-ng Behavior Changes Resource Usage Changes
  • 5. What is Legacy Code? “Code Without Tests”
  • 6. Two Methods of SoTware Development “Edit and Pray” vs. “Cover and Modify” which brings us to…
  • 7. The Legacy Code Dilemma “When we change code, we should have tests in place.” “To put tests in place, we oTen have to change code.” (But why?)
  • 8. Legacy Code Change Algorithm 1. Iden-fy Change Point 2. Find Test Point 3. Break Dependency 4. Write Tests 5. Change and Refactor
  • 9. Seams Exchangeable Behavior + an Enabling Point. Seams let you subs7tute one behavior for another by edi-ng only at the enabling point. Seams let you introduce test collaborators.
  • 10. Object Seams Most useful type of seam for OOP. Another way of talking about polymorphism. Code without seam Code with seam
  • 11. In the Wild No Tests == No Up-­‐to-­‐Date Documenta-on instead of
  • 12. In the Wild Duplicate Code
  • 13. In the Wild Long, Procedural Methods def generate_pdf end
  • 14. In the Wild Stateful Programming • Keeping Track of Flags • HTTP Session Abuse
  • 15. In the Wild Curse of MVC • Fat Models, Fat Controllers • Overloaded Responsibili-es
  • 16. So Many Problems Chapter 6: “I Don’t Have Much Time and I Have to Change It” Chapter 8: “It Takes Forever to Make a Change” Chapter 10: “I Can’t Run This Method in a Test” Chapter 16: “I Don’t Understand the Code Well Enough To Change It” Chapter 17: “My Applica-on Has No Structure” Chapter 19: “My Project Is Not Object Oriented. How Do I Make Safe Changes? Chapter 20: “This Class Is Too Big and I Don’t Want It to Get Any Bigger”
  • 17. This Class is Too Big Methods defined on Job:
  • 18. Single Responsibility Principle “Every class should have a single responsibility: It should have a single purpose in the system, and there should be only one reason to change it.”
  • 20. Seeing Responsibili-es Effect Sketch: Show Internal Rela-onships
  • 21. Seeing Responsibili-es Primary Responsibility “Job presents damper inspec-on data in report form.” “Job generates files of reports.” “Job deletes report files.” “Job produces graphs of damper inspec-on data.” “Job persists and retrieves representa-ons of jobs in a database.”
  • 22. Iden-fy Change Point We want to refactor Job to extract Reporter. 1. Iden-fy Change Point 2. Find Test Point 3. Break Dependency 4. Write Tests 5. Change and Refactor
  • 23. Find Test Point Intercep-on Points Higher Level Tes-ng – don’t have to break dependencies – verifies that your feature actually works – slow, inconvenient – hard to automate – not tes-ng in isola-on Progressive Strategy 1. Iden-fy Change Point 2. Find Test Point 3. Break Dependency 4. Write Tests 5. Change and Refactor
  • 24. Break Dependencies Required for unit tes-ng. Less necessary for higher-­‐level tes-ng. We’re gonna skip it (but just this -me). 1. Iden-fy Change Point 2. Find Test Point 3. Break Dependency 4. Write Tests 5. Change and Refactor
  • 25. Write Tests “Tes-ng” in the Interac-ve Console: 1. Iden-fy Change Point 2. Find Test Point 3. Break Dependency 4. Write Tests 5. Change and Refactor
  • 26. Change and Refactor This was harder than it looks. 1. Iden-fy Change Point 2. Find Test Point 3. Break Dependency 4. Write Tests 5. Change and Refactor
  • 27. Refactored Code Five different extracted classes (plus two more)
  • 29. BeNer With Seam? We can add a seam by injec-ng the dependency on the reporter. This way, we can test in isola-on by passing in a FakeReporter. Unfortunately, this makes our produc-on code ugly:
  • 31. Legacy Code Ain’t So Bad • No writer’s block. • Learn as you go. • Easy scapegoat! • Unavoidable, so learn to love it.
  • 32. Thanks J Working Effec7vely with Legacy Code Michael Feathers Amar Shah @amar47shah
  • 33. because SoTware is Never Done Refactor to Remove Demeter Viola-on