Top Banner
www.odd-e.com Fight Legacy Code Write Unit Test Stanly Lau
35

[XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

Aug 22, 2015

Download

Software

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

www.odd-e.com

Fight Legacy Code!Write Unit Test!

Stanly Lau

Page 2: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

2

I’m Stanly

• Lives in Singapore!• Agile coach, SW developer!• Worked in small to large

companies!• Experienced in Insurance, Mobile

Safety, Education, Logistics and Banking!

• Java, .Net, JavaScript, Objective-C

[email protected]

Page 3: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]
Page 4: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]
Page 5: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

5

What is Legacy Code?!!

The Problem!!

How to deal with it?

Page 6: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

6

Page 7: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

What is legacy code?

7

Page 8: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

Legacy Code is!Code without Unit Test

8

Michael Feathers

Page 9: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

9

It is not a unit test when: • It talks to the database!• It communicates across the network!• It touches the file system!• It can't run at the same time

as any of your other unit tests!• You have to do special things to your

environment (such as editing config files) to run it.

Michael Feathers

Page 10: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

The problem

10

Page 11: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

13

Inheritance nightmare

Page 12: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

14

Code tends to grow

Page 13: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

15

amount of bad

code

panic

amount of code

smells

time spend on

bug fixing

motivation

developers

quick hacks

opportunity for

# of bugs

indicates

O

refactoring

O

Page 14: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

16

Existing Behaviour New

This is where we spent most time on,!preserving behaviour!

Codebase

Page 15: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

17

Productive

Working duration in a day

Does your day looks like this?

Page 16: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

18

Productive =(

Don’t know why the ‘if’ doesn’t work

Page 17: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

19

Productive =(

Stuck on understanding what the

code is doing

Page 18: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

20

Productive =(

Stuck on thinking of a general solution to solve multiple

problems

Page 19: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

21

Productive =(

Realised you misunderstood the requirements

Page 20: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

22

Productive =(

Fixing production bug

Page 21: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

23

Features Estimates

Business Developers

Competitive dynamic

Limits everyone’s perspectives

Page 22: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

24

James Grenning

“Software change is part of being a developer, so you better get used to it. !Even more, you better get good at it!”

Page 23: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

How to deal with it?

25

Page 24: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

26

Modern engineering practices

26

Test-Driven Development

Refactoring

Continuous Integration

Emergent Design

Page 25: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

27

Test-driven development • Increments of functionality!• Always in working state

Code and fix • Large functionalities!• Long time to get to a working statef

t

Page 26: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

The Legacy Code Dilemma

30

When we change code, we should have tests in place. To put tests in place, we often have to change code.

Page 27: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

But I don't understand the code well enough to change It =(

31

Scratch RefactoringNotes/Sketching

Delete Unused Code

Page 28: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

Legacy code algorithm

32

1. Identify change point!2. Find test points!3. Break dependencies!4. Write tests!5. Make changes and refactor

Page 29: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

Exercise:!Dependency Breaking!Characterisation Tests!

!

http://bit.ly/FightLegacyCode

33

Page 30: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

Are we going faster?

34

Feels faster

Feels slower

Page 31: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

35

Kent Beck

“I'm not a great programmer;!I'm just a good programmer with great habits”

Page 32: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

Closing

36

Page 33: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

Books - Technical Practices

37

Page 34: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

38

@avernois

Page 35: [XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]

[email protected] !

@stanlylau

39