2. Contents
The Way We Work
The Way We Have To Work
What is Legacy Code
Working Effectively with Legacy Code
Strategy
Case Study
3. The Way We Work
Quick & Dirty
Slow Down
Alternative
Landing Time
Cycle(3 Years)
4. The Way We Have To Do
Quick & Clean
But We Already Have ...
Work Effectively with Legacy Code
Build Your Value
5. What is Legacy Code
Codes Without Tests
Hard to get tested
New Code : Legacy = 1 : 100(1,000)
Have to make decoupling design changes
No safety net
Catch-22
6. Working Effectively with
Legacy Code
"Find some small part of legacy codes you
can test without making big changes.
Then use those tests to extends the design
changes more safely."
8. Don’t Start Big Project
New Project for Refactoring or Testing
Everything is fine at start
Doing New design
Cause Double-duty
Impossible to replace the old system
9. Wait Until New Feature
is Needed
Change little bit of design
Add a few tests in the affected area
Eventually spread the test through out the
legacy code
Not all but important/vital bits are get
tested
10. For all new code you write
write test first
Option either modify or add a new module
write a new module with TFD
Always Prefer Test First
13. Have to Understand but
Function is Too Big
Function Size(Screenful)
Function Should Do One Thing
Extract Major Sections into Function
Extract Different levels of abstract
14. Extract Different Levels
of Abstraction
Extract Till You Drop
Until simply restates the code without
changing abstraction level
Most basic refactoring technique
Safe with tool support
16. As a Result
Composed Method Pattern
To Paragraphs
All class’s methods are less than 4 lines
{} in if, while stmt removed
Comment removed
Easy to read in top-down way
19. Have to Change
But Not Enough Time
No Time to Break Dependency and Add Tests
TDD for new feature
Solutions
Sprout method / Sprout class
Wrap method / Wrap class
20. Sprout Method
You Need to Add a Feature to a System
Can be formulated completely as new code
Write the code in a new method
Call it from the places where the new
functionality needs to be
22. Steps
Identify where you need to make your code
change.
Write down a call for a new method then
comment it out.
Develop the sprout method using TDD
Remove the comment to enable the call.
26. Need to Make Change
But Don’t Know What Tests to Write
Need to Know What the SW Supposed To Do
Write Tests Based on Those Ideas
Dig up Old Requirements Documents and
Project Memos ???
27. Characterization Test
What the systems does is more important
than what it is supposed to do.
We want preserve behavior
Test that characterizes the actual behavior
of a piece of code.
28. Steps
Write an assertion that you know will fail
Let the failure tell you what the behavior is
Change the test so that it expects the
behavior that the code produces
31. Subclass and Override
Method
A core technique for breaking dependencies
in OOP
Use inheritance in the context of a test
to nullify behavior that you don’t care
about
to get access to behavior that you do care
about
33. Big function hides
classes
Big function
Variables & Arguments
Functional Sections Inside Indents
Class
Set of Variables
Set of Functions Operate on These Variables
35. Extract Method Object
Steps
Move Initialization Logic to constructor
Extract Methods Till You Drop
Move Type to New File
Rename class / method
Method Ordering(Top-Down, To-paragraph)