SlideShare a Scribd company logo
1 of 81
ACCU 2009




                       legacy
                       code
            learning to live with it

                                       Pete Goodliffe
                                       pete@goodliffe.net
                                                            1
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
legacy code learning to live with it         pete goodliffe




                                       Eve



          Adam




                                                          3
legacy code learning to live with it   pete goodliffe




   most software is
                                                    4
most software is
                   5
6
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
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
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
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
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
There is a lot of legacy




                       ?
 code being written
      right now




                           12
?
                       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
is it actually bad?
                                            not necessarily *




* terms and conditions apply
                                                                14
who works with it?
              muggins here that)
                   (good luck with




                           skil s >>
                                       15
helpful traits
    ! bravery




                 16
helpful traits
    ! bravery
    ! memory




                 17
helpful traits
    ! bravery
    ! memory
    ! methodicalness(osity)




                              18
helpful traits
    ! bravery
    ! memory
    ! methodicalness(osity)
    ! imagination




                              19
helpful traits
    ! bravery
    ! memory
    ! methodicalness(osity)
    ! imagination




                              20
helpful traits
    ! bravery
    ! memory
    ! methodicalness(osity)
    ! imagination
    ! patience
    ! intelligence
    ! empathy
    ! experience
    ! persistence
    ! curiosity
    ! application
    ! dedication



                              21
legacy code learning to live with it   pete goodliffe




plan of attack
     ! what is legacy code
     ! how to understand it
     ! how to modify it




                                                   22
<< understanding




                   modifying >>
                                  23
Everything that irritates us
about others can lead us to
 a better understanding of
         ourselves.
                   Carl Jung (1875 - 1961)




                                             24
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
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
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
understand: the software


claptrap)
 ! what platform(s) does it run on?
 ! how is it deployed?
 ! what dev processes is it encumbered by?




                                             28
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
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
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
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
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
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
understand: the code


the code)
this is the real task: mapping the software
  ! the usual approach: guesswork
  ! a better approach: structured investigation




                                                  35
understand: the code

this is the real task: mapping the software
  ! the usual approach: guesswork
  ! a better approach: structured investigation




                                                  36
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
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
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
understand: the code


the map)
find your route in




                         gauge the quality >>
                     as you find a route




                                            40
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
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
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
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
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
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
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
select mapping tools
 ! command line
 ! graphical
 ! programatic




                       48
command line tools
 ! wc -l
 ! grep (-i)
 ! find (-name)
 ! xargs
 ! piping
 ! ls -hF --color -R
 ! find . -name “*.h” -o -name “*.c” | xargs cat | wc -l
 ! find . -name “*.h” -o -name “*.c” | xargs grep -i “usb_debug”

 ! cygwin
 ! ctags (excuberant ctags)
 ! mlcscope

                                                                   49
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
programatic tools
 ! unit test frameworks
 ! continuous integration
 ! refactor-capable editor
 ! source control




                             51
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
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
the caution)
 remember, this is not an
    event, its an ongoing
           process



                            54
legacy code learning to live with it   pete goodliffe




plan of attack
     ! what is legacy code
     ! how to understand it
     ! how to modify it




                                                   55
this is the easy bit
               well, not really




                                  56
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
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
! on
   et
       as
          k   at
                   at
                     im
                        e




                            59
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
write the code




                 61
write the code
               but that’s a different talk...



follow these rules >>


                                                62
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
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
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
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
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
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
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
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
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
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
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
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
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
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
how to modify it
         ! code tact
         ! trust the code
         ! close the feedback loop
         ! meddle methodically




                                     77
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
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
further reading




                  80
legacy code learning to live with it                     pete goodliffe




                                       the end




                                                 Pete Goodliffe
                                                 pete@goodliffe.net
                                                                      81

More Related Content

Similar to Legacy Code: Learning To Live With It

Stash - We Code Together
Stash - We Code TogetherStash - We Code Together
Stash - We Code TogetherSven Peters
 
Kidding Around: Designing Digital Activities (#mw2012)
Kidding Around: Designing Digital Activities (#mw2012)Kidding Around: Designing Digital Activities (#mw2012)
Kidding Around: Designing Digital Activities (#mw2012)Shelley Mannion
 
GOTO Berlin - When Code Cries
GOTO Berlin - When Code CriesGOTO Berlin - When Code Cries
GOTO Berlin - When Code CriesCory Foy
 
Wow We presentation-russian
Wow We presentation-russianWow We presentation-russian
Wow We presentation-russianNatalya Shulga
 
Spring, CDI, Jakarta EE good parts
Spring, CDI, Jakarta EE good partsSpring, CDI, Jakarta EE good parts
Spring, CDI, Jakarta EE good partsJarek Ratajski
 
Many ways to Android Development - a rant!
Many ways to Android Development - a rant!Many ways to Android Development - a rant!
Many ways to Android Development - a rant!kashifrazzaqui
 
W jax wenn-code altert
W jax  wenn-code altertW jax  wenn-code altert
W jax wenn-code altertSven Peters
 
Wow we presentation_russian
Wow we presentation_russianWow we presentation_russian
Wow we presentation_russianIWOWWE
 
Stood at the bottom of a mountain looking up
Stood at the bottom of a mountain looking upStood at the bottom of a mountain looking up
Stood at the bottom of a mountain looking upPete Goodliffe
 
QC Merge 2012: Growing community
QC Merge 2012: Growing communityQC Merge 2012: Growing community
QC Merge 2012: Growing communityLB Denker
 

Similar to Legacy Code: Learning To Live With It (13)

Stash - We Code Together
Stash - We Code TogetherStash - We Code Together
Stash - We Code Together
 
Kidding Around: Designing Digital Activities (#mw2012)
Kidding Around: Designing Digital Activities (#mw2012)Kidding Around: Designing Digital Activities (#mw2012)
Kidding Around: Designing Digital Activities (#mw2012)
 
GOTO Berlin - When Code Cries
GOTO Berlin - When Code CriesGOTO Berlin - When Code Cries
GOTO Berlin - When Code Cries
 
Wow We presentation-russian
Wow We presentation-russianWow We presentation-russian
Wow We presentation-russian
 
Spring, CDI, Jakarta EE good parts
Spring, CDI, Jakarta EE good partsSpring, CDI, Jakarta EE good parts
Spring, CDI, Jakarta EE good parts
 
Progressing and enhancing
Progressing and enhancingProgressing and enhancing
Progressing and enhancing
 
Many ways to Android Development - a rant!
Many ways to Android Development - a rant!Many ways to Android Development - a rant!
Many ways to Android Development - a rant!
 
Code Beauty
Code BeautyCode Beauty
Code Beauty
 
W jax wenn-code altert
W jax  wenn-code altertW jax  wenn-code altert
W jax wenn-code altert
 
Wow we presentation_russian
Wow we presentation_russianWow we presentation_russian
Wow we presentation_russian
 
Stood at the bottom of a mountain looking up
Stood at the bottom of a mountain looking upStood at the bottom of a mountain looking up
Stood at the bottom of a mountain looking up
 
QC Merge 2012: Growing community
QC Merge 2012: Growing communityQC Merge 2012: Growing community
QC Merge 2012: Growing community
 
Blogging Tips
Blogging TipsBlogging Tips
Blogging Tips
 

More from Pete Goodliffe

Becoming a Better Programmer
Becoming a Better ProgrammerBecoming a Better Programmer
Becoming a Better ProgrammerPete Goodliffe
 
Running Effective Worship Rehearsals
Running Effective Worship RehearsalsRunning Effective Worship Rehearsals
Running Effective Worship RehearsalsPete Goodliffe
 
Becoming a Better Programmer (2013)
Becoming a Better Programmer (2013)Becoming a Better Programmer (2013)
Becoming a Better Programmer (2013)Pete Goodliffe
 
Version Control Done Right
Version Control Done RightVersion Control Done Right
Version Control Done RightPete Goodliffe
 
C++: The Cathedral and the Bizarre
C++: The Cathedral and the BizarreC++: The Cathedral and the Bizarre
C++: The Cathedral and the BizarrePete Goodliffe
 
iOS Development (BCS Newcastle)
iOS Development (BCS Newcastle)iOS Development (BCS Newcastle)
iOS Development (BCS Newcastle)Pete Goodliffe
 
Three Objectionable Things
Three Objectionable ThingsThree Objectionable Things
Three Objectionable ThingsPete Goodliffe
 
Coping with Complexity
Coping with ComplexityCoping with Complexity
Coping with ComplexityPete Goodliffe
 
iOS Development (BCS Edinburgh 2011-03-09)
iOS Development (BCS Edinburgh 2011-03-09)iOS Development (BCS Edinburgh 2011-03-09)
iOS Development (BCS Edinburgh 2011-03-09)Pete Goodliffe
 
iPhone development: A brief introduction
iPhone development: A brief introductioniPhone development: A brief introduction
iPhone development: A brief introductionPete Goodliffe
 

More from Pete Goodliffe (15)

Becoming a Better Programmer
Becoming a Better ProgrammerBecoming a Better Programmer
Becoming a Better Programmer
 
Words in Code
Words in CodeWords in Code
Words in Code
 
Running Effective Worship Rehearsals
Running Effective Worship RehearsalsRunning Effective Worship Rehearsals
Running Effective Worship Rehearsals
 
Becoming a Better Programmer (2013)
Becoming a Better Programmer (2013)Becoming a Better Programmer (2013)
Becoming a Better Programmer (2013)
 
Design Sins
Design SinsDesign Sins
Design Sins
 
Advanced iOS
Advanced iOSAdvanced iOS
Advanced iOS
 
Version Control Done Right
Version Control Done RightVersion Control Done Right
Version Control Done Right
 
Getting Into Git
Getting Into GitGetting Into Git
Getting Into Git
 
C++: The Cathedral and the Bizarre
C++: The Cathedral and the BizarreC++: The Cathedral and the Bizarre
C++: The Cathedral and the Bizarre
 
iOS Development (BCS Newcastle)
iOS Development (BCS Newcastle)iOS Development (BCS Newcastle)
iOS Development (BCS Newcastle)
 
Three Objectionable Things
Three Objectionable ThingsThree Objectionable Things
Three Objectionable Things
 
Coping with Complexity
Coping with ComplexityCoping with Complexity
Coping with Complexity
 
Manyfestos
ManyfestosManyfestos
Manyfestos
 
iOS Development (BCS Edinburgh 2011-03-09)
iOS Development (BCS Edinburgh 2011-03-09)iOS Development (BCS Edinburgh 2011-03-09)
iOS Development (BCS Edinburgh 2011-03-09)
 
iPhone development: A brief introduction
iPhone development: A brief introductioniPhone development: A brief introduction
iPhone development: A brief introduction
 

Recently uploaded

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 

Recently uploaded (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
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
  • 3. legacy code learning to live with it pete goodliffe Eve Adam 3
  • 4. legacy code learning to live with it pete goodliffe most software is 4
  • 6. 6
  • 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
  • 16. helpful traits ! bravery 16
  • 17. helpful traits ! bravery ! memory 17
  • 18. helpful traits ! bravery ! memory ! methodicalness(osity) 18
  • 19. helpful traits ! bravery ! memory ! methodicalness(osity) ! imagination 19
  • 20. helpful traits ! bravery ! memory ! methodicalness(osity) ! imagination 20
  • 21. helpful traits ! bravery ! memory ! methodicalness(osity) ! imagination ! patience ! intelligence ! empathy ! experience ! persistence ! curiosity ! application ! dedication 21
  • 22. legacy code learning to live with it pete goodliffe plan of attack ! what is legacy code ! how to understand it ! how to modify it 22
  • 23. << understanding modifying >> 23
  • 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
  • 40. understand: the code the map) find your route in gauge the quality >> as you find a route 40
  • 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
  • 48. select mapping tools ! command line ! graphical ! programatic 48
  • 49. command line tools ! wc -l ! grep (-i) ! find (-name) ! xargs ! piping ! ls -hF --color -R ! find . -name “*.h” -o -name “*.c” | xargs cat | wc -l ! find . -name “*.h” -o -name “*.c” | xargs grep -i “usb_debug” ! cygwin ! ctags (excuberant ctags) ! mlcscope 49
  • 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
  • 54. the caution) remember, this is not an event, its an ongoing process 54
  • 55. legacy code learning to live with it pete goodliffe plan of attack ! what is legacy code ! how to understand it ! how to modify it 55
  • 56. this is the easy bit well, not really 56
  • 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
  • 59. ! on et as k at at im e 59
  • 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
  • 81. legacy code learning to live with it pete goodliffe the end Pete Goodliffe pete@goodliffe.net 81