31
Refactoring Legacy Code Pablo Machado [email protected]

Refactoring legacy code

Embed Size (px)

DESCRIPTION

Slides usados na apresentação sobre Refactoring legacy code.

Citation preview

Page 1: Refactoring legacy code

Refactoring Legacy Code

Pablo [email protected]

Page 2: Refactoring legacy code

Legacy Code?

Code written using old technology?

Code written by somebody who doesn't work for the company anymore?

A code very fragile or complicated?

Page 3: Refactoring legacy code

Legacy Code

➔ Hard to understand and maintain

➔ Intimidating to be changed

Page 4: Refactoring legacy code

Legacy Code?

“To me, legacy code is simply code without tests.”Michael Feathers

Page 5: Refactoring legacy code

Legacy Code?

“If you wrote a new feature today, which took you a lot of mental energy and time, and tomorrow you'll be afraid to change it …”

Page 6: Refactoring legacy code

Refactoring?

Page 7: Refactoring legacy code

Why Refactor?

➔ To understand the code

➔ To improve the health of code-base

➔ To be confident

➔ To optimize for readability

Page 8: Refactoring legacy code

Why Refactor?

➔ To understand the code

➔ To improve the health of code-base

➔ To be confident

➔ To optimize for readability

Page 9: Refactoring legacy code
Page 10: Refactoring legacy code

The Fear Factor

Page 11: Refactoring legacy code

Unit Test

Characterization tests

Page 12: Refactoring legacy code

Refactor Dilemma

When we refactor, we should have tests.

To test we often have to refactor.

Page 13: Refactoring legacy code

Legacy Code Change

Identify Change Points

Identify Test Points

Break Dependecies

Write Test

Refactor

Page 14: Refactoring legacy code

Working Effectively With

Legacy Code

Page 15: Refactoring legacy code

Working Effectively With

Legacy Code

Baby Steps

Page 16: Refactoring legacy code

Working Effectively With

Legacy Code

Invest only where necessary

Page 17: Refactoring legacy code

Working Effectively With

Legacy Code

Know how to use the tools

Page 18: Refactoring legacy code
Page 19: Refactoring legacy code

Breaking Dependencies

Page 20: Refactoring legacy code

External Dependencies

Production Code

Page 21: Refactoring legacy code

External Dependencies

Production Code

Test Code

Page 22: Refactoring legacy code

External Dependencies

Test Code

Production Code

Page 23: Refactoring legacy code

Internal/Hidden

dependencies

Production Code Refactor To Test

Page 24: Refactoring legacy code

Test Code

Production Code Test Code

Internal/Hidden

dependencies

Page 25: Refactoring legacy code

Refactor Technique

Page 26: Refactoring legacy code

Sprout Method

When?Add a feature as completely new code in a untestable method.

How?Identify the inputs and outputs of the new methodCreate a call for the new methodDevelop the new method with TDD

Page 27: Refactoring legacy code

Sprout Method

AdvantagesTest new codeSepareta new cold from the old

DisadvantagesDont test old codeDont make the code better

Page 28: Refactoring legacy code

Wrap Class

When?Add a feature as completely new code in complex class.

How?Create a class and extend the old classOverride the old method and make a callDevelop the new method with TDD

Page 29: Refactoring legacy code

Sprout Method

AdvantagesTest new codeSepareta new cold from the old

DisadvantagesDont test old codeDont make the code better

Page 30: Refactoring legacy code

Referências

http://refactoring.com/

Page 31: Refactoring legacy code