ddc2011 효과적으로레거시코드다루기

42
효과적으로 레거시 코드 다루기 서비스플랫폼개발팀 백명석

Upload: myeongseok-baek

Post on 13-Apr-2017

154 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Ddc2011 효과적으로레거시코드다루기

효과적으로 레거시 코드 다루기

서비스플랫폼개발팀백명석

Page 2: Ddc2011 효과적으로레거시코드다루기

Contents

The Way We Work

The Way We Have To Work

What is Legacy Code

Working Effectively with Legacy Code

Strategy

Case Study

Page 3: Ddc2011 효과적으로레거시코드다루기

The Way We Work

Quick & Dirty

Slow Down

Alternative

Landing Time

Cycle(3 Years)

Page 4: Ddc2011 효과적으로레거시코드다루기

The Way We Have To Do

Quick & Clean

But We Already Have ...

Work Effectively with Legacy Code

Build Your Value

Page 5: Ddc2011 효과적으로레거시코드다루기

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

Page 6: Ddc2011 효과적으로레거시코드다루기

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."

Page 7: Ddc2011 효과적으로레거시코드다루기

Strategy

Don’t Start Big Project

Wait Until New Feature is Needed

Always Prefer Test First

Page 8: Ddc2011 효과적으로레거시코드다루기

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

Page 9: Ddc2011 효과적으로레거시코드다루기

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

Page 10: Ddc2011 효과적으로레거시코드다루기

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

Page 11: Ddc2011 효과적으로레거시코드다루기

One More Automation by Tools

Page 12: Ddc2011 효과적으로레거시코드다루기

Case Study

Extract Method

Sprout Method

Characterization Test

Subclass and Override Method

Extract Method Object

Page 13: Ddc2011 효과적으로레거시코드다루기

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

Page 14: Ddc2011 효과적으로레거시코드다루기

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

Page 15: Ddc2011 효과적으로레거시코드다루기

Extract Method Example

Forbidden Keyword Check Controller

Page 16: Ddc2011 효과적으로레거시코드다루기

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

Page 17: Ddc2011 효과적으로레거시코드다루기

Before

Page 18: Ddc2011 효과적으로레거시코드다루기

After

Page 19: Ddc2011 효과적으로레거시코드다루기

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

Page 20: Ddc2011 효과적으로레거시코드다루기

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

Page 21: Ddc2011 효과적으로레거시코드다루기

Sprout Method Structure

Page 22: Ddc2011 효과적으로레거시코드다루기

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.

Page 23: Ddc2011 효과적으로레거시코드다루기

Sprout Method Example

Add Synchronized Token Pattern to EditArticleController

Page 24: Ddc2011 효과적으로레거시코드다루기

Test

Page 25: Ddc2011 효과적으로레거시코드다루기

Production

Page 26: Ddc2011 효과적으로레거시코드다루기

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 ???

Page 27: Ddc2011 효과적으로레거시코드다루기

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.

Page 28: Ddc2011 효과적으로레거시코드다루기

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

Page 29: Ddc2011 효과적으로레거시코드다루기

Characterization Test Example

Xml Request String Factory

Page 30: Ddc2011 효과적으로레거시코드다루기

Remove Unwanted Dependency

Too complex or impossible to mock out

Prefer no mocking in terms of better design

Page 31: Ddc2011 효과적으로레거시코드다루기

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

Page 32: Ddc2011 효과적으로레거시코드다루기

Subclass and Override Method Example

Static Function

global variable like as singleton

Page 33: Ddc2011 효과적으로레거시코드다루기

Big function hides classes

Big function

Variables & Arguments

Functional Sections Inside Indents

Class

Set of Variables

Set of Functions Operate on These Variables

Page 34: Ddc2011 효과적으로레거시코드다루기

Extract Method Object

Steps

Extract Method(invoke)

Create New Inner Class

Convert Arg. to Field

Convert Local Var. to Field

Page 35: Ddc2011 효과적으로레거시코드다루기

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)

Page 36: Ddc2011 효과적으로레거시코드다루기

Extract Method Object Example

ArticleController#buildRelatedContents

Page 37: Ddc2011 효과적으로레거시코드다루기

As a Result

Large Ugly Tangled Function

Extracted into More than 1 classes

Which are Ready to Refactor

Page 38: Ddc2011 효과적으로레거시코드다루기

Before

Page 39: Ddc2011 효과적으로레거시코드다루기

After

Page 40: Ddc2011 효과적으로레거시코드다루기

Etc.

Adding New Feature

TDD / Programming by Difference

Reuse What

Study Test

2 Way TDD

Page 41: Ddc2011 효과적으로레거시코드다루기

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

Page 42: Ddc2011 효과적으로레거시코드다루기

Q & A