The presentation given at the ACCU 2009 conference.
Synopsis...
Legacy code. You can't live with it. You can't live without it.
Well, you can't avoid it, at least. Spend long enough in the software factory, and you'll inevitably run into other people's old code. And of course, none of this old stuff is any good. It's nothing like the high quality software you craft. Pure tripe.
Let's be honest, sometimes you might even stumble across some of *your own* old code, and embarrassing as it is, you have to admit that you don't know how it works, let alone how to fix it.
This presentation will look at practical strategies for working with "old" crufty code. We'll see how to:
start working with a completely unfamiliar codebase
understand old spaghetti programming
make correct modifications
prevent bad code from causing more pain in the future
Automating Google Workspace (GWS) & more with Apps Script
Legacy Code: Learning To Live With It
1. ACCU 2009
legacy
code
learning to live with it
Pete Goodliffe
pete@goodliffe.net
1
2. legacy code learning to live with it pete goodliffe
Pete Goodliffe
A programmer, a columnist, an
author, a teacher. Someone who
cares about code. Books: Code
Craft, Beautiful Architecture.
www.goodliffe.net
goodliffe.blogspot.com
Pete Goodliffe
pete@goodliffe.net
2
7. legacy code learning to live with it pete goodliffe
talk synopsis
Legacy code. You can't live with it. You can't live without it.
Well, you can't avoid it, at least. Spend long enough in the software factory,
and you'll inevitably run into other people's old code. And of course, none
of this old stuff is any good. It's nothing like the high quality software
you craft. Pure tripe.
Let's be honest, sometimes you might even stumble across some of your own
old code, and embarrassing as it is, you have to admit that you don't know
how it works, let alone how to fix it.
This presentation will look at practical strategies for working with “old”
crufty code. We'll see how to:
! start working with a completely unfamiliar codebase
! understand old spaghetti programming
! make correct modifications
! prevent bad code from causing more pain in the future
7
8. legacy code learning to live with it pete goodliffe
plan of attack
! what is legacy code
! how to understand it
! how to modify it
8
9. legacy code learning to live with it pete goodliffe
plan of attack
! what is legacy code
! how to understand it
! how to modify it
9
10. legacy (noun)
1. Law. a gift of property, esp.
personal property, as money, by
will; a bequest.
?
2. anything handed down from
the past, as from an ancestor or
predecessor: the legacy of
ancient Rome.
10
11. wha
de
de ng co e er
pli n
co sti
od rite sup io
ld xi
e c ’t w
O e by vers
y -dat n
An -of d orted uct
di p
t
ut you up od
Oe pr
is leg
s
od nger vious s
C lo
re t test y
No m a p ou
?
log
ro with chno
F
de ld te
acy
Co s o
e code
Us d”
Ba
“
cod
e?
11
12. There is a lot of legacy
?
code being written
right now
12
13. ?
re
a
cs changbee exte ed
e nd
wement eds to d
o
d
y
h ed
r
ui de ne vere e fix
w eq co co to b
Rd
! Ol re dis eds s ed
rt
?
e
a e ne ng be po
sd
ug co ha to
Bd y c ds
! Ol olog e
n e ne
ech cod
Td
! Ol
13
14. is it actually bad?
not necessarily *
* terms and conditions apply
14
15. who works with it?
muggins here that)
(good luck with
skil s >>
15
24. Everything that irritates us
about others can lead us to
a better understanding of
ourselves.
Carl Jung (1875 - 1961)
24
25. you
hav
e to
und
! th
! th e so e
rsta
ft
! th e ch war
nd
! ho e co ange e yo
w t de y s yo u a
o a ou u m re c
pp ar us han
roa e c t m gin
ch han ak g
the gin e
co g
de
25
26. understand: the software
software)
! what type of software is it?
! e.g. shrinkwrap, server, bespoke
! what does it do?
! what does it do? really?
! have you used it?
! howQA isit tested?
is
! what there?
! is there documentation?
! are there manuals?
! gauge the quality (e.g. bug count, reliability)
26
27. understand: the software
people)
! who has domain expertise?
! do you need domain expertise?
! who wrote it?
! who owns it?
! what’s the license?
! who are the users?
! are they technical?
! have they been involved in development?
27
28. understand: the software
claptrap)
! what platform(s) does it run on?
! how is it deployed?
! what dev processes is it encumbered by?
28
29. understand: the software
changes)
! where is it stored?
! change control
! where is the repository (what system)
! trunk/branching strategy
! feature/release/personal branching
! who can commit, when as you?
! who else is working on the same branch
! can you break build?
29
30. understand: the software
malarkey)
! other procedural tools
! bug tracker?
!bug management process?
! who manages?
! who hands out bugs?
! who gives you an account?
! continuous integration
! testing process
!how thorough?
! is it automated?
30
31. understand: your approach
attitude)
the right attitude
Weakness of attitude becomes weakness of character.
Albert Einstein
! don’t freak out!
! someone once understood it
! conquer disgust
! you can improve it
31
32. understand: your approach
strategise)
strategise
become effective by being selective
! how much time do you have to work with it?
! affects how you work a route through it
! how long will you be working with it for?
! how much of it do you need to know?
32
33. understand: the changes
mission)
what do you have to do?
Do not, for one repulse, forego the purpose that you
resolved to effect.
William Shakespeare, ‘The Tempest’
! what was the old behaviour?
! what will the new behaviour be?
! how will you know you are done?
33
34. understand: the changes
mission)
what do you have to do?
! is it a single coding task?
! or ongoing work in the system?
! drive-by programming?!
! will you take responsibility for whole section of
code?
! are you on a schedule?
! do you agree with work packages?
34
35. understand: the code
the code)
this is the real task: mapping the software
! the usual approach: guesswork
! a better approach: structured investigation
35
36. understand: the code
this is the real task: mapping the software
! the usual approach: guesswork
! a better approach: structured investigation
36
37. understand: the code
the map)
#1: the basic facts
! the language(s)
! and the language version (e.g. C# 2.0, C89, Python 2.0)
! the size files, age (does this seem in keeping with project?)
! LOC, classes,
! the build technology
! check every build variant
! how its deployed
! main technologies
!libraries
! database(s)?
! design tools
! validation/QA tools
! external dependencies
37
38. understand: the code
the map)
build it. now.
! don’t go any further until you’ve got it cleanly built and running
! only then can you modify anything sanely
38
39. understand: the code
the map)
find your route in
! is the code structure
! data-centric
! control-centric
! does the system decompose into parts?
!for separate build
! for separate use
! which bits do you need to look at now?
! can you ask someone?
39
41. rk
o e? ?
w lik nd
ss ok fi
ue lo to
g ld ct
ou xpe
y
b sh e ap
ng it ou
t
or ink y
pi rm
sh
re t ou
p o
t
irs you ons d el: y
a e f do
m cti l mod
th at
! wh subse ta
en
! wha a m
t
! build
!
user interface
business logic
av libs database
operating system
41
42. e
c ts
rfa oin
te ace
in erf
int
y e pbsystem aces
s
b acre su
ng rf
rf he
pi te
te w in dth
instem
p hebrea l
ifyin sy
a f tlity,
nt ces
m dee pla ure oqua ve
ih -le
! ! t nat , style low ,
hehnology vel / ap
t ec
! ! t h-le ur m
ig e yo
hn
! refi
!
ui/bl goo
user interface &
business logic media access db veneer
database
av libs database mp3 aac wav
operating system
operating system
42
43. re
tu
c
u nsight
tr
s
fileluab ei t
y
c
l oje
b f pr ct
g ive a va re o roje
in
u
uct of p
p str re
p can g nal uctu
a
r
nte l str ject
m s i na
re
o
hownter of pr
! tu
er s of i lity
uc
eith ack qua
ss: de tr
! or l for
s
ce co ry
! clues
oe to
pr th
!
ec es
e dir ur
th ind
! .f ct
e ?
e
ru
th tur
st
ruc
1 ot t st
pl D h projec mon
2. E atc
Q es it m e com
3. ! do s t shape
niojec
og pr
ecGNU
r
! ! IDE
s
!
43
44. te
ra
pa
ontions se
ti ”
c
se w “sec
y
b
ng ho te?
pi ne ara
p i
a rm sep
ete ey
m d th
! do level s
! ! higuirograads
m
h
til
p
! thre ies
un
us t
! libr t
ar s
io ec
ec
! proj l
bv roj
s
! leve space
’t o e p
s
tion
w me s
! ! n kage onve kup db isn db
lo a n
is th th
pac ing c t mar
th wi
! nam en
async audio asyncmaudio es d
! co m
tim ke
! e
m wor
so ’ve
!
sync audio you
(real time)
44
45. y el?
nc
e l mod
nd
e
p ra
e
d ctu
y iter
b ch filte tch?
ng ar ipe/
pi ee nt, p s ma
s ne
p oumpo ncie ools
a y co
m anered, dees withrtts
c ay
! ! l depenenci , impo cy
en
end des
o ce dep nclu nd
d ra #i
! ! t ollow phs depeace on
ti
of of inter f
a
ga
! f call gr y ity ing
lit ual upl pa
!
ua to q / co t pro
q ied on
! ! t ohesi ffec
se
!c
ap
m
!
45
46. w
flo ?
l ol?
ro
nt y point b of tr
on
o c
c
y e entr in” hu
b th a
ng
pi
p is he “m
a re s t ss
m he e i roce ace
w er h p
! wh r, batc f
ter
t in
nen
! ! linvent ge qu ork, c ? threads? nt?
ea loop eue ompo
edare the oncurre
! emessa amew
adlled afe? be c
rentro d s n’t
fr
! app
thl co rea & ca
!
it wel ly th an
is ow tual at c
! ! his it ac ear whrities
! is it cl prio
! thread
!
46
47. ry
to de
is o
hc
y
b
ng ge he
pi ft ?
p o ?
a n?
ed ified
t
ea tar mod
m io
it s st
rs
th as t la ntrol
ve
! w
en was i n co
st
it lly
wh en ?
rs?
! wh revisio te uff
te na
tho ) st
la
ro gi
l
u
! mine ya efu
he ri
w on? g (us
an
t
! o
ho
m
ve ing stin
r/
m
ork tere
w tho a ro
! h w
u
ef
ea ou ve in
ou re y s ha
on
! y am
a
do
s
ch che
tc see
am
nn
a
! ei
n
e
t br r bra er t lice
a
rc oth
wh othe h to
g?
! do or /
u blis
oin
so end , pu
!
he sg
v
eam
d/
t ti
r
! loa pst
ei
n
it u
ow
er
d
ubm
!
wh res
,
! nal
ter
in
!
47
50. graphical tools
! code visualisation (modeling)
! doxygen, Ndoc
! a good IDE
! profiler
! debugger (not so good in large projects)
! understand for C++
testing
! static analysis
! code test (lints, gcc -Wall)
! code coverage (clover, coverlipse)
! purify
! valgrindcachegrind, callgrind, kcachegrind, etc)
!(memcheck,
50
51. programatic tools
! unit test frameworks
! continuous integration
! refactor-capable editor
! source control
51
52. understand: the code
the map) -diagrams
keep notes
-keep them updated
! notebook -what’s wrong
! wiki -bits that don’t fit
! text files
-things to look at later in
more detail
-bits to fix later
-record progress
-unanswered questions
52
53. understand: the code
the map)
gauge quality
! structure
! appropriateness
! cohesion/coupling
! single responsibility
! code quality
!readability
! for separate use
! the of building
build
!ease
! documentation
! automated (automatable)
! does it build without warnings?
53
57. you
rm
issio
! ma
! do ke n
n’t the
! im b c
pro rea han
ve k a ges
the nyt
co hin
de g
on
the
wa
y
57
58. you
rm
issio
! wh
! on at a n
! w e ta re th
ha sk
! fi t e at e req
! re x bug lse d a ti uir
o y me eme
! in facto s
! on tegr r ou nts
ne ?
e t ate ed
ask to
at do
at ?
im
e
58
60. ge
an
ch e s?
ge
to ng an
de ha ch
co r c (s) by
e fo
t th ns ion ected
strat
oin atio funct aff te
inp loc xact es? opria
be erfac pr
Pe
t n to e ight int ap
o
N ow e m nging ge is
els cha
D
egis
an
tu
ha yo ch
of
W re
nd
ki le
A ery
at ert rg e
hf su lac
e
W ee art rep rface? ypes
W n he d
n d inte rotot
e
Op up a ol
yp
tr
p n
Ri intai t:
en
a
M rim
pe
Ex
60
62. write the code
but that’s a different talk...
follow these rules >>
62
63. tyle
ct
ta
e
d ies
s
o ing ibrar
:c ist , l
1 rs
ex ng me t
# e
th ami ly
le am no
w
llo ut, n careful progr or
ru Fo yo aries
! La d libr rlier round y
ea ill a efull
ct st
Ad
pe er ar
es th e c st
R he
! W he cod e bea de
co
he
t t agil ft
ea fr ho
Tr a it
! It’s lite to muc e
oo a tim
o
e p ask t t
B ’t a
on thing
De
! On
63
64. rule #
2: kno
w wh
!D on’t
o to
trus
Reb t the
uild
trust
, ma build
Esp
! Not ecially ke cle syste
if ha an, d m
the
earl
Kee s cu epe
! Not p the b ier pro stom ndenc
gram step ies
ene
the
spec fit o mer s
Doc
! Onl umen ificati f the do s
ts g ons
y th ubt
et o
e co
Wh utda
de
at it ted
Kno doe
wh s rig
ow ht n
to a ow
sk it
64
65. p
loo
!B
uil
ck
Ho d it
! B w .R
rea lo u
ba
Ju k i ng n it
! D st t do . Te
o o to
d es st
Th ne pro it t it.
ee
ch en y thin ve ak Re
! C an o g yo e? pe
on ge u k at u’ at.
f
no a t ve c
str
Do
the
n’t uct w im ha
se 3:
wh e ng
sta a te at
clo #
ed
b i st ma it
n t en
de
rule
he vir
da onm th
e
rk en
t
65
66. p
loo
!A
vo
Sp id s
ck
ee wi
He d tch
ba
En lps up ing
ab yo tur
Pr u g na out
d ev les
Slo en ex et ro
ee
w ts pe int und
er rim o f
En
Sw cou tu
itc rag rn ro en low
f
rs
a
hin es
g t mu r ta
as lti ou
the
tio
nd
ks pl
se 3:
n
be e s
ee ult ki
tw im
clo #
n b an ll
uil eo s
ds us de
an ve
rule
ch
lo
ge
pm
s
en
t
66
67. p
loo
!P
ro
No ve
ck
Ne thin you
Yo w f g w r c h
ba
! H u un as a
ow hav ctio br nge
d tes d e d na oke s w
tin o y on lity n or
ee
Ne ou e w w k
g
ed
! u sa do hat ork
! a nit t go th wa s
f
is? s r
cc es od
the
ep ts eq
co
se 3:
ta uir
ve
nc rin ed
clo #
et g
es
ts
rule
67
68. tests)
! don’t need to create 100% test coverage!
! more tests better than fewer
! broad coverage for main parts of functionality
! a few broad tests probably more effective than many narrow ones
! targeted tests for the piece you’re changing
! test-first for new code
! addingmockable interfaces easy
tests is not
!break out
! find/create seams to inspect behaviour
! refactor
! easier for OO code than procedural
! explore existing functionality
!capture them in tests!
68
69. ly
a
ic
d de
o
th
co
ut
: me d o de
4e te co
# dl se men ld
le d ou om
ru e /o
h
the ave c ssary in
y le ive
id ’t e
ec rly ol
m Tn
! Do e unn lea like t
ry
t nt c u’d
ele e ssa ge
m as yo sion ce an
D
ne ch
m it
Co ve u at is r
ntr wh fo
ea ise i e es
Lm
fac
g ler e
i
n chan nter
i pi ak
M ly t id
! On om m
ouxten lity e c ou
akand e tiona on th p y
rerap unc n r hel
B W tf
lea ile
rou
ss: mp
Sp
ne co
zi
La t the
! Le es
ng
ha
c 69
70. ly
a
ic
d de
o
th
co
ut
: me d o de
4e te co
# dl se men ld
le d ou om
ru e
class Frog /o
h
the ave c ssary in
{ y le ive
id ’t c); ece y ol
m Tn
! Do e unn learl like t
void Paint(Colour
ry
t nt c u
Food FavouriteFood() const; ’d
ele e ssa ge
m as yo sion ce an
D
ne ch
}; m it
Co ve u at is r
ntr wh fo
ea ise i e es
Lm
fac
g ler e
i
n chan nter
i pi ak
M ly t id
! On om m
ouxten lity e c ou
akand e tiona on th p y
rerap unc n r hel
B W tf
lea ile
rou
ss: mp
Sp
ne co
zi
La t the
! Le es
ng
ha
c 70
71. ly
a
ic
d de
o
th
co
ut
: me d o de
4e te co
# dl se men ld
le d ou om
ru e
class Frog /o
h
the ave c ssary in
{ y le ive
id ’t c); ece y ol
m Tn
! Do e unn learl like t
void Paint(Colour
ry
t nt c u
Food FavouriteFood() const; ’d
ele e ssa ge
yo ce an
static ColourD mm
GetLivery(Food f);ion ne ch
as rus
Co ve it nt is r
at fo
};
ea ise i e wh ces
Lm
ng terfa ler e
i
n cha n
i pi ak
M ly t id
! On om m
ouxten lity e c ou
akand e tiona on th p y
rerap unc n r hel
B W tf
lea ile
rou
ss: mp
Sp
ne co
zi
La t the
! Le es
ng
ha
c 71
72. ly
a
ic
d de
o
th
co
ut
: me d o de
4e te co
# dl se men ld
le d ou om
ru e
class Frog /o
h
the ave c ssary in
{ y le ive
id ’t c); ece y ol
m Tn
! Do e unn learl like t
void Paint(Colour
ry
t nt c u
Food FavouriteFood() const; ’d
ele e ssa ge
yo ce an
static ColourD mm
GetLivery(Food f);ion ne ch
as rus
Co ve it nt is r
at fo
};
ea ise i e wh ces
Lm
ng terfa ler e
i
n cha n
i pi a
Colour c = Frog::GetLivery(freddie.FavouriteFood()); k
M ly t id
! On om m
ouxten lity e c ou
freddie.Paint(c);
akand e tiona on th p y
rerap unc n r hel
B W tf
lea ile
rou
ss: mp
Sp
ne co
zi
La t the
! Le es
ng
ha
c 72
73. ly
a
ic
d de
o
th
co
ut
: me d o de
4e te co
# dl se men ld
le d ou om
ru e
class Frog /o
h
the ave c ssary in
{ y le ive
id ’t c); ece y ol
m Tn
! Do e unn learl like t
void Paint(Colour
ry
t nt c u
Food FavouriteFood() const; ’d
ele e ssa ge
yo ce an
static ColourD mm
GetLivery(Food f);ion ne ch
as rus
Co ve it nt is r
at fo
};
ea ise i e wh ces
Lm
ng terfa ler e
i
n cha n
i pi a
Colour c = Frog::GetLivery(freddie.FavouriteFood()); k
M ly t id
! On om m
ouxten lity e c ou
freddie.Paint(c);
akand e tiona on th p y
rerap unc n r hel
B W tf
lea ile
rou
ss: mp
Sp
ne co
zi
La t the
! Le es
ng
ha
c 73
74. ly
a
ic
d de
o
th
co
ut
: me d o de
4e te co
# dl se men ld
le d ou om
ru e
class Frog /o
h
the ave c ssary in
{ y le ive
id ’t e
void XXX_Paint(Colourec rly ol
m Tn
! Do e unn lea like t
c);
ry
tn
void PaintInLivery(); t c
ele e const;ou’d ssa ge
ce an
D
Food FavouriteFood() s y
ma n
io is ne ch
m it us at
Co ve ntr w or
static Colour GetLivery(Food f); h
ea ise i e sf
ce
Lm
}; g
n terfa ler e
i
n cha n
i pi ak
M ly t id
! On om m
ouxten lity e c ou
Colour c = Frog::GetLivery(freddie.FavouriteFood());
akand e tiona on th p y
freddie.Paint(c); //<Brerapto unc
fails compile n r hel
lea ile
f
W out
ss: mp
r
Sp
ne co
zi
La t the
! Le es
ng
ha
c 74
75. ly
a
ic
d de
o
th
co
ut
: me d o de
4e te co
# dl se men ld
le d ou om
ru e
class Frog /o
h
the ave c ssary in
{ y le ive
id ’t e
void XXX_Paint(Colourec rly ol
m Tn
! Do e unn lea like tc);
ry
tn
void PaintInLivery(); t c
ele e const;ou’d ssa ge
ce an
D
Food FavouriteFood() s y
ma n
io is ne ch
m it us at
Co ve ntr w or
static Colour GetLivery(Food f); h
ea ise i e sf
ce
Lm
}; g
n terfa ler e
i
n cha n
i pi ak
M ly t id
! On om m
freddie.PaintInLivery(); k ouxten ity e c ou
eap and ectional on th lp y
BrWra t fun an er he
le il
u
ss: mp
pro
S
ne co
zi
La t the
! Le es
ng
ha
c 75
76. ly
a
ic
d de
o
th
co
ut
: me d o de
4e te co
# dl se men ld
le d ou om
ru e
class Frog /o
h
the ave c ssary in
{ y le ive
id ’t c); ece y ol
m Tn
! Do e unn learl like t
void Paint(Colour
ry
tn
void PaintInLivery(); t c
ele e const;ou’d ssa ge
ce an
D
Food FavouriteFood() s y
ma n
io is ne ch
m it us at
Co ve ntr w or
static Colour GetLivery(Food f); h
ea ise i e sf
ce
Lm
}; g
n terfa ler e
i
n cha n
i pi ak
M ly t id
! On om m
freddie.PaintInLivery(); k ouxten ity e c ou
eap and ectional on th lp y
BrWra t fun an er he
le il
u
ss: mp
pro
S
ne co
zi
La t the
! Le es
ng
ha
c 76
77. how to modify it
! code tact
! trust the code
! close the feedback loop
! meddle methodically
77
78. legacy code learning to live with it pete goodliffe
plan of attack
! what is legacy code
! how to understand it
! how to modify it
78
79. legacy code learning to live with it pete goodliffe
lessons to learn
! new code becomes old instantly
! write code that’s easy to modify
! prevent errors in the future
leave a legacy: test suite
!
make your code heard to misinterpret
!
strive for clear interfaces and sound structure
!
file structure follows code structure
!
increase development speed
!
take small verifiable steps: one thing at a time
!
learn from legacy code to make new code better
!
79