Re Factoring Legacy Code

Embed Size (px)

Citation preview

  • 8/6/2019 Re Factoring Legacy Code

    1/25

    Refactoring Legacy SystemsRefactoring Legacy Systems

    Willem van den Ende [email protected] Evers - [email protected]

    2007 Living Software & Piecemeal Growth Some Rights Reserved

    Piecemeal GrowthPiecemeal Growth

    Living Software B.V.Living Software B.V.

  • 8/6/2019 Re Factoring Legacy Code

    2/25

    A designer knows he has achieved perfection

    not when there is nothing left to add,but when there is nothing left to take away.

    Antoine de Saint-Exupry

  • 8/6/2019 Re Factoring Legacy Code

    3/25

    3

    About UsDesign Debt Cycle

    Refactoring Legacy CodeApproaches

    DemoQ&A

  • 8/6/2019 Re Factoring Legacy Code

    4/25

    4

    Who we areWho we are

    Willem van den Ende

    Refactoring

    1996 New Legacy Application

    Teams

    Independent (Living Software B.V.)

    All-hands person

    Software development coach Trainer

    Consultant

    blog: me.andering.com

  • 8/6/2019 Re Factoring Legacy Code

    5/25

    5

    Who we areWho we are

    Marc EversMarc Evers

    Independent (Piecemeal Growth)Independent (Piecemeal Growth)

    Software development coachSoftware development coach

    TrainerTrainer

    ConsultantConsultant

    Blog: blog.piecemealgrowth.netBlog: blog.piecemealgrowth.net

  • 8/6/2019 Re Factoring Legacy Code

    6/25

    6

    What we doWhat we do

    More business value from software developmentMore business value from software development

    andand

    helping others do ithelping others do it

    throughthrough

    Coaching & mentoringCoaching & mentoring

    TrainingTraining

    FacilitationFacilitation

    Organizing conferencesOrganizing conferences

  • 8/6/2019 Re Factoring Legacy Code

    7/25

    7

    Legacy CodeLegacy Code

  • 8/6/2019 Re Factoring Legacy Code

    8/25

    8

    Legacy CodeLegacy Code

    Legacy code = code without tests

  • 8/6/2019 Re Factoring Legacy Code

    9/25

    9

    Design Debt CycleDesign Debt Cycle

  • 8/6/2019 Re Factoring Legacy Code

    10/25

    10

    There is light at the end of the tunnel!There is light at the end of the tunnel!

  • 8/6/2019 Re Factoring Legacy Code

    11/25

    Refactoring Legacy CodeRefactoring Legacy Code

    Improving the internal structure of softwareImproving the internal structure of softwarewithout changing its external behaviourwithout changing its external behaviour

    Tiny, safe, almost mechanical stepsTiny, safe, almost mechanical steps

    Unit tests!Unit tests!

  • 8/6/2019 Re Factoring Legacy Code

    12/25

    Refactoring approachesRefactoring approaches

    Cut at the seamsCut at the seams

    Reveal intentionsReveal intentions

    Use your toolsUse your tools One smell at a timeOne smell at a time

    Pairs & peersPairs & peers

    Exploratory refactoringExploratory refactoring Sometimes it gets worse before it gets betterSometimes it gets worse before it gets better

  • 8/6/2019 Re Factoring Legacy Code

    13/25

    Cut at the seamsCut at the seams

    Seam = place where you can alter behaviourSeam = place where you can alter behaviour

    in a program without editing in that placein a program without editing in that place

    Seam typesSeam types

    Preprocessing seamPreprocessing seam

    Link seamLink seam

    Object seamObject seam

    Break dependenciesBreak dependencies

    To add testsTo add tests

  • 8/6/2019 Re Factoring Legacy Code

    14/25

    Object seam exampleObject seam examplepublic class Spreadsheet {public class Spreadsheet {

    public Spreadsheet buildSheet(Cell cell) {public Spreadsheet buildSheet(Cell cell) {

    ......

    recalculate(cell);recalculate(cell);

    ......

    }}

    public void recalculate(Cell cell) { ... }public void recalculate(Cell cell) { ... }

    ......

    }}

    public class TestingSpreadsheet extends Spreadsheet {public class TestingSpreadsheet extends Spreadsheet {

    public void recalculate(Cell cell) {public void recalculate(Cell cell) {

    ......

    }}

    }}

    (based on(based on Working Effectively with Legacy CodeWorking Effectively with Legacy Code, p. 42/43), p. 42/43)

  • 8/6/2019 Re Factoring Legacy Code

    15/25

    Reveal intentionsReveal intentions

    Write code for who comes after you,not for the computer

  • 8/6/2019 Re Factoring Legacy Code

    16/25

    Use your toolsUse your tools

    Canned refactoringsCanned refactorings

    Extract method, extract local variableExtract method, extract local variable

    Rename method, rename class, rename variableRename method, rename class, rename variable

    Extract interfaceExtract interface

    Code analysisCode analysis

    Eclipse compiler warnings/errorsEclipse compiler warnings/errors

    PMDPMD

    Basic code hygieneBasic code hygiene

  • 8/6/2019 Re Factoring Legacy Code

    17/25

    One smell at a timeOne smell at a time

    Code smellCode smell

    warning sign aboutwarning sign aboutpossible problempossible problem

    God classGod classLong methodLong method

    Duplicated codeDuplicated code

    Intention hiding namesIntention hiding names

    Focus!Focus!

    Refactoring backlogRefactoring backlog

  • 8/6/2019 Re Factoring Legacy Code

    18/25

    Pairs & peersPairs & peers

  • 8/6/2019 Re Factoring Legacy Code

    19/25

    SometimesSometimes

    it gets worseit gets worse

    DefactoringDefactoring

    Temporarily use not-so-good practicesTemporarily use not-so-good practices

    before itbefore it

    gets bettergets better

  • 8/6/2019 Re Factoring Legacy Code

    20/25

    Refactor as you goRefactor as you go

    Exploratory RefactoringExploratory Refactoring

    Clean what you're working onClean what you're working on

    Always leave it in a better state than when youAlways leave it in a better state than when you

    startedstarted

    Add TestsAdd Tests

    Persevere!Persevere!

  • 8/6/2019 Re Factoring Legacy Code

    21/25

    DemoDemo

    CoffeeMUDCoffeeMUD

    www.zimmers.net/home/mud/www.zimmers.net/home/mud/

    sourceforge.net/projects/coffeemudsourceforge.net/projects/coffeemud

    Multi User DungeonMulti User Dungeon JavaJava

    3066 classes3066 classes

    33210 methods33210 methods

    340 Kloc340 Kloc

    Open sourceOpen source

  • 8/6/2019 Re Factoring Legacy Code

    22/25

    CoffeeMUDCoffeeMUD

    MOB = any creature in the worldMOB = any creature in the world

    StdMOBStdMOB

    Smells: god class, large class, long methodsSmells: god class, large class, long methods

    Collaborators: Ability, Behavior, Item, Faction, DeityCollaborators: Ability, Behavior, Item, Faction, Deity

    Interfaces: MsgListener, EnvironmentalInterfaces: MsgListener, Environmental

    bli b l kM (E i t l H t CMM ) {

  • 8/6/2019 Re Factoring Legacy Code

    23/25

    public boolean okMessage(Environmental myHost, CMMsg msg) {

    try {

    checkNotDeity(msg);

    checkCharacterStats(msg);

    checkEffects(msg);

    checkInventories(msg);

    checkBehaviours(msg);

    checkFactions(msg);

    MOB mob = msg.source();

    checkImmortalityAndLimbs(msg, mob);checkConsequencesOfRange(msg, mob);

    if (targetHasEffect(msg) && (msg.amITarget(this))) {

    checkIAmPresentInWorld();

    performCombat(msg, mob);

    if ((rangeToTarget() >= 0) && (!isInCombat()))setAtRange(-1);

    checkCanDoActionToTarget(msg, mob);

    }

    invokeDepressionAroundMyDeath(msg);

    ...

    }

  • 8/6/2019 Re Factoring Legacy Code

    24/25

    ReferencesReferences

    Martin Fowler,Martin Fowler, RefactoringRefactoring

    William C. Wake,William C. Wake, Refactoring WorkbookRefactoring Workbook

    Micheal Feathers,Micheal Feathers, Working Effectively withWorking Effectively withLegacy CodeLegacy Code

  • 8/6/2019 Re Factoring Legacy Code

    25/25

    CopyrightCopyright

    This work is licensed under a Creative Commons Attribution-Share Alike 3.0This work is licensed under a Creative Commons Attribution-Share Alike 3.0Netherlands license - http://creativecommons.org/licenses/by-sa/3.0/nl/Netherlands license - http://creativecommons.org/licenses/by-sa/3.0/nl/

    Selbsthass Schnurrbart (CC Attribution-Share alike)Selbsthass Schnurrbart (CC Attribution-Share alike)

    http://flickr.com/photos/schnurrbart/49048321/http://flickr.com/photos/schnurrbart/49048321/

    light at the end of the tunnel BotheredByBees (CC Attribution)light at the end of the tunnel BotheredByBees (CC Attribution)

    http://flickr.com/photos/botheredbybees/1465128757http://flickr.com/photos/botheredbybees/1465128757

    house moving Karen Apricot (CC Attribution-Share Alike)house moving Karen Apricot (CC Attribution-Share Alike)http://flickr.com/photos/karenapricot/402072480/http://flickr.com/photos/karenapricot/402072480/

    Brown trekkyandy (CC Attribution-Share alike)Brown trekkyandy (CC Attribution-Share alike)

    http://flickr.com/photos/trekkyandy/1236266577http://flickr.com/photos/trekkyandy/1236266577

    Them tools Jurek D. (CC Attribution)Them tools Jurek D. (CC Attribution)

    http://flickr.com/photos/jurek_durczak/311837936/http://flickr.com/photos/jurek_durczak/311837936/

    Smelly Fish deejaymarlon (CC Attribution)Smelly Fish deejaymarlon (CC Attribution)

    http://flickr.com/photos/deejaymarlon/428580031/http://flickr.com/photos/deejaymarlon/428580031/

    Bernardo e Elmar. Improve It (CC Attribution-Share Alike)Bernardo e Elmar. Improve It (CC Attribution-Share Alike)

    http://flickr.com/photos/improveit/1681875499/http://flickr.com/photos/improveit/1681875499/

    Look what I have done! Elsie esq. (CC Attribution)Look what I have done! Elsie esq. (CC Attribution)

    http://flickr.com/photos/elsie/485356846/http://flickr.com/photos/elsie/485356846/