refactoring legacy code - alvaro...

44
Refactoring Legacy Code @alvarobiz, 2015-06-29

Upload: others

Post on 26-May-2020

38 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

Refactoring Legacy Code

@alvarobiz, 2015-06-29

Page 2: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

INTRODUCTION

TO LEGACY CODE

Page 3: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

What is legacy code

[…] legacy code as code without tests. It is a good working definition, and it points to a solution […]

M Feathers, in the preface of Working effectively with legacy code

Page 4: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

What is legacy code (II)

Legacy code is code without

tests that provide trust to all

your stakeholders

Page 5: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

What is tested code

Trust (reliable)

Cheap to modify

Page 6: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

CHANGING

LEGACY CODE

Page 7: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

How to get from A to B

Shortest

Safest

Page 8: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

Any long-lived software

0% 20% 40% 60% 80% 100%

Features/ LoC

To Keep To Change

Page 9: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

How to change from A to B

Keep old, risk new

Risk old, keep new

Page 10: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

Any long-lived software (II)

0% 20% 40% 60% 80% 100%

Features/ LoC

To Keep To Change

Page 11: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

THE

LEGACY CODE CHANGE

ALGORITHM

Page 12: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

The Legacy Code Change Algorithm

Identify change points.

Find test points.

Break dependencies.

Write tests.

Make changes and refactor.

Source: [Feathers04], Ch 2: Working with Feedback

Page 13: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

Legacy Code Change, example

Example: Ugly Trivia

We want to remove the

direct printing to the console (System.out.printXXX)

Page 14: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

The Legacy Code Change Algorithm

Identify change points.

Find test points.

Break dependencies.

Write tests.

Make changes and refactor.

Source: [Feathers04], Ch 2: Working with Feedback

Page 15: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

Change points

All direct invocations to

System.out.printXXX

Page 16: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

The Legacy Code Change Algorithm

Identify change points.

Find test points.

Break dependencies.

Write tests.

Make changes and refactor.

Source: [Feathers04], Ch 2: Working with Feedback

Page 17: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

Test points

The console output

Page 18: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

The Legacy Code Change Algorithm

Identify change points.

Find test points.

Break dependencies.

Write tests.

Make changes and refactor.

Source: [Feathers04], Ch 2: Working with Feedback

Page 19: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

Breaking dependencies

Are the execution

reproducible?

Page 20: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

The Legacy Code Change Algorithm

Identify change points.

Find test points.

Break dependencies.

Write tests.

Make changes and refactor.

Source: [Feathers04], Ch 2: Working with Feedback

Page 21: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

Writing tests

Capture the console output

Save and verify it

automatically

Page 22: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

The Legacy Code Change Algorithm

Identify change points.

Find test points.

Break dependencies.

Write tests.

Make changes and refactor.

Source: [Feathers04], Ch 2: Working with Feedback

Page 23: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

Making changes

Global search and replace

“System.out.println” for “log”

into an object

Verify, commit

Page 24: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

Refactoring

Inject the logging

collaborator

Verify, commit

Page 25: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

Refactoring

DRY on the messages

Verify, commit

Page 26: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

The Legacy Code Change Algorithm

Identify change points.

Find test points.

Break dependencies.

Write tests.

Make changes and refactor.

Source: [Feathers04], Ch 2: Working with Feedback

Page 27: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

The Legacy Code Change Algorithm

Rewriting Adapting Writing

Cost

Page 28: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

UNDERSTANDING

LEGACY CODE

Page 29: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

Sensing

[S]ense when we can’t

access values our code

computes

Source: [Feathers04]:

Chapter 3: Sensing and Separation

Page 30: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

Separating

[S]eparate when we can’t

even get a piece of code

into a test harness to run.

Source: [Feathers04]:

Chapter 3: Sensing and Separation

Page 31: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

Solutions

Fake Objects

Mock Objects

Seams

Dependency breaking

Source: [Feathers04]:

Chapter 3: Sensing and Separation

Page 32: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

EXPERIENCES

WITH

LEGACY CODE

Page 33: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

Motivation (I)

Page 34: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

Motivation (II)

I have nothing to

offer but blood, toil,

tears and sweat.

W. Churchill

Page 35: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

How to get from A to B

One step at a time

Page 36: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

How to get from A to B

Low-hanging fruit

Page 37: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

How to get from A to B

Refactor relentlessly

Page 38: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

How to get from A to B

Do not stop the

presses, prefer

along the way

Page 39: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

How to get from A to B

Make it usable, then

forget for a while

Page 40: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:
Page 41: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

GETTING

(PROFESSIONAL)

HELP

Page 42: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:
Page 43: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:
Page 44: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:

Bibliography

[Feathers04]: Feathers, M. Working Effectively

with Legacy Code, ISBN-13: 007-6092025986.

Amazon

[Refactoring]: Fowler, M with Beck, Brant, Opdyke,

and Roberts. Refactoring: Improving the Design of

Existing Code,ISBN-13: 978-0201485677 Official

page

[Kerievsky04]: Kerievsky, J. Refactoring to Patterns.

ISBN-13: 078-5342213355 Official page