Here’s the codebase
About Me
• Clean coder, TDDer, Software Craftsman
• Over 12 years of development
• Lead the infrastructure operations and development
workflows at StartApp
• Tech lead and scrum master (eBay)
[email protected]
http://eyalgo.com/
@eyalgo_egolan
OOD
Open mind
Attitude over technique
About the Presentation
What is Legacy Code?
“…Legacy code refers to an application
system source code that is
no longer supported…” (techopedia)
This is Reality
New features…
Modify existing behavior…
Removing features…
Fixing bugs…
Working With Legacy Code
Modify behavior of existing
(unfamiliar) code
Identify requirements
Understand the code
Modify the code
I changed the ‘if’ with a
‘switch’. Is there a new
bug?
Why Tests?
Risky change
Refactoring, new feature, bug
Feedback
Assurance and confidence
Summing Up
We need to modify code
We want safety using tests
Summing Up
We need to modify code
We want safety using tests
The code does not have
tests
What is Legacy Code ? (revised)
“Code without tests”Michael Feathers
(Working Effectively with Legacy Code)
What is Legacy Code ? (revised)
“Code without tests”Michael Feathers
(Working Effectively with Legacy Code)
Untestable code
Process/pro-cess/
def: A number of actions or
functions done in order to
produce a desired result.
Identify requirements
Understand the code
Write tests
Modify the code
Identify requirements Understand the code
Write tests Modify the code
Working with Legacy Code
Working With Legacy Code (revised)
Obstacles
I don’t understand it
I don’t have time
Unstructured code
Breaking dependencies
I Don’t Understand the Code
Debugging…
I Don’t Understand the Code
I Don’t Understand the Code
Extract Methods
Move fields
Create classes
Throw that code away…
I Don’t Understand the Code
Difficult to Instantiate the Class
Difficult to Instantiate the Class
Expose Static Method (which does not use
instance’s variables)
1. Write test that access the method to
be tested
2. Extract the body to a static method
3. Call the static method from the
original one
Adapt Complicated Class
Adapter
Test
with
fake
Object Creation in the Constructor
Object Creation in the Constructor
In your test: create a subclass for testing
Static Methods
Test this
Fake this
Static Methods
Create a wrapping instance method
Fake this
Singleton
Introduce a setter for ‘instance’
Factory Method
1
Inject fake server
Hidden Method
• Test through a public method
• Extract to a new class
• Expose the method
Did I Break Something?
Do one thing at a time
Preserve signature
Trust your computer and IDE
Pair programming
Huge Method
• Usually not following SRP
• Usually with too many dependencies
• Invest time and create a safety harness
• Refactor: extract to smaller, tested methods, extract class
More Operations
Introduce / Extract interface
Rename (better understanding)
Expose visibility (package protected)
Extract and override
Tools
Use your IDE
mockito
PowerMock and EasyMock
Pair Programming
Preliminary Knowledge
OOD
SOLID
Unit testing frameworks
Clean code
Refactoring principles
Ask Yourself
1. What changes do we have to make?
2. How will we know that we’ve done
them correctly?
3. How will we know we haven’t
broken anything?
I Don’t Have Time
Always leave the place cleaner
than you found it
Boy scouts Rule
Q & A
Thank You
[email protected]