ddc2011 효과적으로레거시코드다루기
TRANSCRIPT
효과적으로 레거시 코드 다루기
서비스플랫폼개발팀백명석
Contents
The Way We Work
The Way We Have To Work
What is Legacy Code
Working Effectively with Legacy Code
Strategy
Case Study
The Way We Work
Quick & Dirty
Slow Down
Alternative
Landing Time
Cycle(3 Years)
The Way We Have To Do
Quick & Clean
But We Already Have ...
Work Effectively with Legacy Code
Build Your Value
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
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."
Strategy
Don’t Start Big Project
Wait Until New Feature is Needed
Always Prefer Test First
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
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
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
One More Automation by Tools
Case Study
Extract Method
Sprout Method
Characterization Test
Subclass and Override Method
Extract Method Object
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
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
Extract Method Example
Forbidden Keyword Check Controller
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
Before
After
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
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
Sprout Method Structure
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.
Sprout Method Example
Add Synchronized Token Pattern to EditArticleController
Test
Production
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 ???
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.
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
Characterization Test Example
Xml Request String Factory
Remove Unwanted Dependency
Too complex or impossible to mock out
Prefer no mocking in terms of better design
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
Subclass and Override Method Example
Static Function
global variable like as singleton
Big function hides classes
Big function
Variables & Arguments
Functional Sections Inside Indents
Class
Set of Variables
Set of Functions Operate on These Variables
Extract Method Object
Steps
Extract Method(invoke)
Create New Inner Class
Convert Arg. to Field
Convert Local Var. to Field
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)
Extract Method Object Example
ArticleController#buildRelatedContents
As a Result
Large Ugly Tangled Function
Extracted into More than 1 classes
Which are Ready to Refactor
Before
After
Etc.
Adding New Feature
TDD / Programming by Difference
Reuse What
Study Test
2 Way TDD
ReferenceWorking Effectively with Legacy Code
http://build.gaia.daum.net/wiki/study:working_effectively_with_legacy_code
Clean Coders
http://build.gaia.daum.net/wiki/study:clean-coders-codecasts
TDD by Kent beck
http://build.gaia.daum.net/wiki/study:tyrant
토비의 스프링 3
Q & A