SlideShare una empresa de Scribd logo
1 de 33
Descargar para leer sin conexión
Working with legacy code

        Andrea Polci
Legacy code: definition



“source code inherited from someone else and source
 code inherited from an older version of the software”
                      (wikipedia)

                 “code without tests”
                  (Michael Feathers)
Legacy code: caracteristics
●   Poor architecture
●   Stratification of modifications
●   Non-uniform coding styles
●   Poor written documentation
●   “Oral” documentation lost
●   No tests
●   Extremely valuable!
    ●   Only successful code become legacy code
Why we have legacy code?
● More and more features
● Shortcuts and hacks


● Developer rotation


● Development team growth


    ●   less communication
Why we need to change legacy
              code?
● New functionality
● Bug


● Refactoring


● Optimization
Options
●   Start from scratch?
●   Look for a new Job?
●   May be we need
    Rambo?
●   Or Mc Gyver?
●   May be we need
    some “tools” to work
    effectively with
    legacy code
Test

●   They gives feedback       ●   What about legacy
    on changes                    code?
●   Different kind of tests       ●   To modify it we need
                                      tests
    ●   Unit
                                  ●   To write test we need
    ●   Integration                   to modify the code
    ●   Functional
An algorithm
1) Identify what to change
2) Identify what to test
3) Break dependencies
4) Write the tests
5) Modify and refactoring
What to change

1) Identify what to change
   ●   Do we have enough knowledge to choose where
       to make changes?
   ●   Sometimes we need to modify many different
       places just to make a simple change.
2) Identify what to test
3) Break dependencies
4) Write the tests
5) Modify and refactoring
I don't understand the code

●   Note/Sketching
●   Listing Markup
●   Scratch Refactoring
●   Write tests
●   Delete Unused Code
    ●   There is the repository for that
I don't understand the structure
●   Long lived applications tend to loose structure
    ●   It takes long time to understand the big picture
    ●   There is no big picture
    ●   The team is in emergency mode and lose sight of the
        big picture
●   It's important that every developer understand the
    big picture or the code will diverge from the
    architecture
    ●   Tell the story of the system
    ●   Naked CRC
    ●   Conversation Scrutiny
What to test
1) Identify what to change
2) Identify what to test
   •   Can be an hard work
   •   Effect analisys
   •   How much time do we have?
3) Break dependencies
4) Write the tests
5) Modify and refactoring
I don't have the time to test
●   Be careful!
●   Sometimes there is no other option
●   Don't make it worse!
    ●   Try to isolate new (tested) code from legacy code
    ●   Sprout method/class
    ●   Wrap method/class
Sprout method
public class ProductLablePrinter{
  …
  public void printLabel(int productId) {
    String barcode;
    …
    // compute barcode
    …
    // print barcode
  }
}
Sprout method
public class ProductLablePrinter{
  …
  public void printLabel(int productId) {
    String barcode;
    …
    // compute barcode
    …

        logPrinted(barcode);

        // print barcode
    }

    protected void logBarcode(String barcode) {
      // My code
    }


}
Break Dependencies
1) Identify what to change
2) Identify what to test
3) Break dependencies
   ●   How do I put a class in a test harness?
   ●   How I know I'm not breaking anything?
4) Write the tests
5) Modify and refactoring
Sensing & Separation
●   Sensing:
    we break dependencies to sense when we can't
    access values our code computes
●   Separation:
    we break dependencies to separate when we
    can't even get a piece of code into a test
    harness to run
Sensing & Separation: Example


public class ProductLablePrinter{
  …
  Public void printBarcode(int productId) {
    String barcode;
    …
    // compute barcode
    …
    // print barcode;
  }
}
Seam

●   Seam:
    a place where you can alter behavior of your
    program without editing in that place
●   Enabling Point:
    Every seam has an enabling point, a place
    where you can make the decision to use one
    behaviour or another
●   Looking for existings seams in legacy code
    allow us to break dependencies (for sensing
    or separation) without refactoring.
Different kind of Seams

●   Preprocessor seams

●   Link seam

●   Object seam
Object seam: Example
public class ProductLablePrinter{
  …
  public void printLabel(int productId) {
    String barcode;
    …
    // compute barcode
    …
    printBarcode(barcode);
  }

    protected void printBarcode(String barcode) {
      // access to printer
    }
}

Public void testPrintBarcode() {
  ProductLablePrinter plp = new ProductLabelPrinter(){
    String lastPrinted = null;
    protected void printBarcode(String barcode) {lastPrinted=barcode;}
  }

    // … test code
}
I can't get this class into a Test

●   Objects of the class can't be created easily
    ●   Parameters we have to pass to the constructor
    ●   Hidden dependencies
●   The test harness won't easily build with the
    class in it
●   The constructor we need to use has bad side
    effects
●   Significant work happens in the constructor
    and we need to sense it
Example (1)
public void testLabelPrinter() {
    new LabelPrinter();
}


The constructor LabelPrinter is undefined
Example (2)
public class LabelPrinter {
  public LabelPrinter(Printer prn, Warehose wh) {
    …
    this.printer = prn;
    if(!this.printer.isOnline()) {
      throw new …
    }
  }
}

public void testLabelPrinter() {
  Printer prn = new Printer(“stampante”);
  Warehouse wh = new Warehouse(“magazzino1”);
  LabelPrinter labPrint = new LabelPrinter(prn, wh);
}

public class Printer {
  boolean isOnline(){ … }
  void startJob() { … }
  void printLine(String line) { … }
  void endJob() { … }
}
Example (3)
public interface PrinterInterface {
  boolean isOnline();
  void startJob();
  void printLine(String line);
  void endJob();
}

public class Printer implements PrinterInterface {
  …
}

public class LabelPrinter {
  public LabelPrinter(PrinterInterface prn, Warehose wh) {
    ...
  }
}
I can't run this method in a test
●   Method not accessible to the test
●   It's hard to construct the parameters
●   Side effects (database, access to
    hardware, ecc.)
●   Need to sense through objects used by the
    method
Test
1) Identify what to change
2) Identify what to test
3) Break dependencies
4) Write the tests
5) Modify and refactoring
Modify and refactoring
1) Identify what to change
2) Identify what to test
3) Break dependencies
4) Write the tests
5) Modify and refactoring
   ●   TDD
   ●   Programming by difference
Making changes takes too much
                time
●   Understanding
    ●   How can we increase our understanding of the code?
●   Lag Time
    ●   It takes to much time to see the effect of changes and
        this slow down the development
    ●   Built time
    ●   Slow tests
    ●   No unit tests
    ●   Often to solve this we need to break dependecies
Tools
●   Authomatic refactoring tools
    ●   Can we trust them?
●   Mock Objects
●   Unit test harnesses
    ●   JUnit
●   Other test harnesses
    ●   Non-unit tests
Conclusions
●   No “silver bullet” here
●   It's an hard work but (usually) not
    impossible
●   At first it will seems overwhelming, but
    things will get better as the number of
    tests increase
●   Be pragmatic!
Questions?
References
●   Workking Effectively with Legacy Code,
    Michael C. Feathers
●   Joel on Software: Things you should never do,
    part I
      http://www.joelonsoftware.com/articles/fog0000000069.html

●   Michael Dubakov: Refactoring vs Rewrite
      http://www.targetprocess.com/blog/2009/11/refactoring-vs-rewrite.html

Más contenido relacionado

La actualidad más candente

Process models
Process modelsProcess models
Process modelsStudent
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoringkim.mens
 
Software Development Life Cycle Model
Software Development Life Cycle ModelSoftware Development Life Cycle Model
Software Development Life Cycle ModelJ.T.A.JONES
 
Code refactoring
Code refactoringCode refactoring
Code refactoringLalit Kale
 
Chapter 13 software testing strategies
Chapter 13 software testing strategiesChapter 13 software testing strategies
Chapter 13 software testing strategiesSHREEHARI WADAWADAGI
 
Code Smells and Its type (With Example)
Code Smells and Its type (With Example)Code Smells and Its type (With Example)
Code Smells and Its type (With Example)Anshul Vinayak
 
Coding standard and coding guideline
Coding standard and coding guidelineCoding standard and coding guideline
Coding standard and coding guidelineDhananjaysinh Jhala
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy CodeNaresh Jain
 
Unit iii(part c - user interface design)
Unit   iii(part c - user interface design)Unit   iii(part c - user interface design)
Unit iii(part c - user interface design)BALAJI A
 
Introduction to Software Project Management
Introduction to Software Project ManagementIntroduction to Software Project Management
Introduction to Software Project ManagementReetesh Gupta
 
Software maintenance Unit5
Software maintenance  Unit5Software maintenance  Unit5
Software maintenance Unit5Mohammad Faizan
 
Waterfall Model PPT in Software Engineering
Waterfall Model PPT in Software EngineeringWaterfall Model PPT in Software Engineering
Waterfall Model PPT in Software EngineeringRaju Sheoran
 
Regression testing
Regression testingRegression testing
Regression testingHarsh verma
 
ppt on sOFTWARE DEVELOPMENT LIFE CYCLE
 ppt on sOFTWARE DEVELOPMENT LIFE CYCLE ppt on sOFTWARE DEVELOPMENT LIFE CYCLE
ppt on sOFTWARE DEVELOPMENT LIFE CYCLESwarnima Tiwari
 
Gof design pattern
Gof design patternGof design pattern
Gof design patternnaveen kumar
 
Design concept -Software Engineering
Design concept -Software EngineeringDesign concept -Software Engineering
Design concept -Software EngineeringVarsha Ajith
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean ArchitectureBadoo
 

La actualidad más candente (20)

Process models
Process modelsProcess models
Process models
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
 
Sanity testing and smoke testing
Sanity testing and smoke testingSanity testing and smoke testing
Sanity testing and smoke testing
 
Software Development Life Cycle Model
Software Development Life Cycle ModelSoftware Development Life Cycle Model
Software Development Life Cycle Model
 
Code refactoring
Code refactoringCode refactoring
Code refactoring
 
Chapter 13 software testing strategies
Chapter 13 software testing strategiesChapter 13 software testing strategies
Chapter 13 software testing strategies
 
Code Smells and Its type (With Example)
Code Smells and Its type (With Example)Code Smells and Its type (With Example)
Code Smells and Its type (With Example)
 
Unified process Model
Unified process ModelUnified process Model
Unified process Model
 
Coding standard and coding guideline
Coding standard and coding guidelineCoding standard and coding guideline
Coding standard and coding guideline
 
Quality software models
Quality software modelsQuality software models
Quality software models
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
Unit iii(part c - user interface design)
Unit   iii(part c - user interface design)Unit   iii(part c - user interface design)
Unit iii(part c - user interface design)
 
Introduction to Software Project Management
Introduction to Software Project ManagementIntroduction to Software Project Management
Introduction to Software Project Management
 
Software maintenance Unit5
Software maintenance  Unit5Software maintenance  Unit5
Software maintenance Unit5
 
Waterfall Model PPT in Software Engineering
Waterfall Model PPT in Software EngineeringWaterfall Model PPT in Software Engineering
Waterfall Model PPT in Software Engineering
 
Regression testing
Regression testingRegression testing
Regression testing
 
ppt on sOFTWARE DEVELOPMENT LIFE CYCLE
 ppt on sOFTWARE DEVELOPMENT LIFE CYCLE ppt on sOFTWARE DEVELOPMENT LIFE CYCLE
ppt on sOFTWARE DEVELOPMENT LIFE CYCLE
 
Gof design pattern
Gof design patternGof design pattern
Gof design pattern
 
Design concept -Software Engineering
Design concept -Software EngineeringDesign concept -Software Engineering
Design concept -Software Engineering
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean Architecture
 

Destacado

Working with Legacy Code
Working with Legacy CodeWorking with Legacy Code
Working with Legacy CodeEyal Golan
 
Software Operation Knowledge
Software Operation KnowledgeSoftware Operation Knowledge
Software Operation KnowledgeDevnology
 
Legacy: как победить в гонке (Joker)
Legacy: как победить в гонке (Joker)Legacy: как победить в гонке (Joker)
Legacy: как победить в гонке (Joker)Victor_Cr
 
Club of anonimous developers "Refactoring: Legacy code"
Club of anonimous developers "Refactoring: Legacy code"Club of anonimous developers "Refactoring: Legacy code"
Club of anonimous developers "Refactoring: Legacy code"Victor_Cr
 
LetsGrow SOLID Software development
LetsGrow SOLID Software developmentLetsGrow SOLID Software development
LetsGrow SOLID Software developmentPatrick Kalkman
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestSeb Rose
 
Devon 2011-b-5 효과적인 레거시 코드 다루기
Devon 2011-b-5 효과적인 레거시 코드 다루기Devon 2011-b-5 효과적인 레거시 코드 다루기
Devon 2011-b-5 효과적인 레거시 코드 다루기Daum DNA
 
Dealing with Legacy PHP Applications
Dealing with Legacy PHP ApplicationsDealing with Legacy PHP Applications
Dealing with Legacy PHP ApplicationsClinton Dreisbach
 
PHP 7.1 : elegance of our legacy
PHP 7.1 : elegance of our legacyPHP 7.1 : elegance of our legacy
PHP 7.1 : elegance of our legacyDamien Seguy
 
Dealing With Legacy PHP Applications
Dealing With Legacy PHP ApplicationsDealing With Legacy PHP Applications
Dealing With Legacy PHP ApplicationsViget Labs
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Codescidept
 
Transforming legacy PHP applications with Symfony2 and Varnish
Transforming legacy PHP applications with Symfony2 and VarnishTransforming legacy PHP applications with Symfony2 and Varnish
Transforming legacy PHP applications with Symfony2 and VarnishCraig Marvelley
 
Pimp legacy PHP apps with Apigility - TrueNorthPHP 2014
Pimp legacy PHP apps with Apigility - TrueNorthPHP 2014Pimp legacy PHP apps with Apigility - TrueNorthPHP 2014
Pimp legacy PHP apps with Apigility - TrueNorthPHP 2014Michelangelo van Dam
 
XPDays Ukraine: Legacy
XPDays Ukraine: LegacyXPDays Ukraine: Legacy
XPDays Ukraine: LegacyVictor_Cr
 
XP Days Ukraine 2014 - Refactoring legacy code
XP Days Ukraine 2014 - Refactoring legacy codeXP Days Ukraine 2014 - Refactoring legacy code
XP Days Ukraine 2014 - Refactoring legacy codeDmytro Mindra
 
ITGM#4 Технический долг 2.0
ITGM#4 Технический долг 2.0ITGM#4 Технический долг 2.0
ITGM#4 Технический долг 2.0Maxim Shulga
 
From Legacy to DDD in PHP | Tech Talks | Privalia
From Legacy to DDD in PHP | Tech Talks | PrivaliaFrom Legacy to DDD in PHP | Tech Talks | Privalia
From Legacy to DDD in PHP | Tech Talks | PrivaliaJordi Vila Gallardo
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy CodeRowan Merewood
 
Metadata and ontologies
Metadata and ontologiesMetadata and ontologies
Metadata and ontologiesDavid Lamas
 
Refactoring 101
Refactoring 101Refactoring 101
Refactoring 101Adam Culp
 

Destacado (20)

Working with Legacy Code
Working with Legacy CodeWorking with Legacy Code
Working with Legacy Code
 
Software Operation Knowledge
Software Operation KnowledgeSoftware Operation Knowledge
Software Operation Knowledge
 
Legacy: как победить в гонке (Joker)
Legacy: как победить в гонке (Joker)Legacy: как победить в гонке (Joker)
Legacy: как победить в гонке (Joker)
 
Club of anonimous developers "Refactoring: Legacy code"
Club of anonimous developers "Refactoring: Legacy code"Club of anonimous developers "Refactoring: Legacy code"
Club of anonimous developers "Refactoring: Legacy code"
 
LetsGrow SOLID Software development
LetsGrow SOLID Software developmentLetsGrow SOLID Software development
LetsGrow SOLID Software development
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under Test
 
Devon 2011-b-5 효과적인 레거시 코드 다루기
Devon 2011-b-5 효과적인 레거시 코드 다루기Devon 2011-b-5 효과적인 레거시 코드 다루기
Devon 2011-b-5 효과적인 레거시 코드 다루기
 
Dealing with Legacy PHP Applications
Dealing with Legacy PHP ApplicationsDealing with Legacy PHP Applications
Dealing with Legacy PHP Applications
 
PHP 7.1 : elegance of our legacy
PHP 7.1 : elegance of our legacyPHP 7.1 : elegance of our legacy
PHP 7.1 : elegance of our legacy
 
Dealing With Legacy PHP Applications
Dealing With Legacy PHP ApplicationsDealing With Legacy PHP Applications
Dealing With Legacy PHP Applications
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
Transforming legacy PHP applications with Symfony2 and Varnish
Transforming legacy PHP applications with Symfony2 and VarnishTransforming legacy PHP applications with Symfony2 and Varnish
Transforming legacy PHP applications with Symfony2 and Varnish
 
Pimp legacy PHP apps with Apigility - TrueNorthPHP 2014
Pimp legacy PHP apps with Apigility - TrueNorthPHP 2014Pimp legacy PHP apps with Apigility - TrueNorthPHP 2014
Pimp legacy PHP apps with Apigility - TrueNorthPHP 2014
 
XPDays Ukraine: Legacy
XPDays Ukraine: LegacyXPDays Ukraine: Legacy
XPDays Ukraine: Legacy
 
XP Days Ukraine 2014 - Refactoring legacy code
XP Days Ukraine 2014 - Refactoring legacy codeXP Days Ukraine 2014 - Refactoring legacy code
XP Days Ukraine 2014 - Refactoring legacy code
 
ITGM#4 Технический долг 2.0
ITGM#4 Технический долг 2.0ITGM#4 Технический долг 2.0
ITGM#4 Технический долг 2.0
 
From Legacy to DDD in PHP | Tech Talks | Privalia
From Legacy to DDD in PHP | Tech Talks | PrivaliaFrom Legacy to DDD in PHP | Tech Talks | Privalia
From Legacy to DDD in PHP | Tech Talks | Privalia
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
Metadata and ontologies
Metadata and ontologiesMetadata and ontologies
Metadata and ontologies
 
Refactoring 101
Refactoring 101Refactoring 101
Refactoring 101
 

Similar a Working With Legacy Code

Test driven development - Zombie proof your code
Test driven development - Zombie proof your codeTest driven development - Zombie proof your code
Test driven development - Zombie proof your codePascal Larocque
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code cleanBrett Child
 
TDD in Python With Pytest
TDD in Python With PytestTDD in Python With Pytest
TDD in Python With PytestEddy Reyes
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkPeter Kofler
 
Software Testing Basic Concepts
Software Testing Basic ConceptsSoftware Testing Basic Concepts
Software Testing Basic Conceptswesovi
 
TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)
TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)
TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)ssusercaf6c1
 
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)Nacho Cougil
 
TDD - Seriously, try it! (updated '22)
TDD - Seriously, try it! (updated '22)TDD - Seriously, try it! (updated '22)
TDD - Seriously, try it! (updated '22)Nacho Cougil
 
May 2021 Spark Testing ... or how to farm reputation on StackOverflow
May 2021 Spark Testing ... or how to farm reputation on StackOverflowMay 2021 Spark Testing ... or how to farm reputation on StackOverflow
May 2021 Spark Testing ... or how to farm reputation on StackOverflowAdam Doyle
 
Indy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleIndy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleikram_ahamed
 
Test Driven Development with PHP
Test Driven Development with PHPTest Driven Development with PHP
Test Driven Development with PHPRogério Vicente
 
Introduction of Tools for providing rich user experience in debugger
Introduction of Tools for providing rich user experience in debuggerIntroduction of Tools for providing rich user experience in debugger
Introduction of Tools for providing rich user experience in debuggerNaoto Ono
 
TDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and GomegaTDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and GomegaEddy Reyes
 
Behaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About TestingBehaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About Testingdn
 
Bdd and-testing
Bdd and-testingBdd and-testing
Bdd and-testingmalcolmt
 
TDD - Seriously, try it! - Bucarest Tech Week
TDD - Seriously, try it! - Bucarest Tech WeekTDD - Seriously, try it! - Bucarest Tech Week
TDD - Seriously, try it! - Bucarest Tech WeekNacho Cougil
 
Deliberate Practice, New Learning Styles (2015)
Deliberate Practice, New Learning Styles (2015)Deliberate Practice, New Learning Styles (2015)
Deliberate Practice, New Learning Styles (2015)Peter Kofler
 
Top Tips Every Notes Developer Needs To Know
Top Tips Every Notes Developer Needs To KnowTop Tips Every Notes Developer Needs To Know
Top Tips Every Notes Developer Needs To KnowKathy Brown
 

Similar a Working With Legacy Code (20)

Test driven development - Zombie proof your code
Test driven development - Zombie proof your codeTest driven development - Zombie proof your code
Test driven development - Zombie proof your code
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
 
TDD in Python With Pytest
TDD in Python With PytestTDD in Python With Pytest
TDD in Python With Pytest
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test Framework
 
Software Testing Basic Concepts
Software Testing Basic ConceptsSoftware Testing Basic Concepts
Software Testing Basic Concepts
 
Spock pres
Spock presSpock pres
Spock pres
 
TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)
TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)
TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)
 
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
TDD - Seriously, try it! (updated '22)
TDD - Seriously, try it! (updated '22)TDD - Seriously, try it! (updated '22)
TDD - Seriously, try it! (updated '22)
 
May 2021 Spark Testing ... or how to farm reputation on StackOverflow
May 2021 Spark Testing ... or how to farm reputation on StackOverflowMay 2021 Spark Testing ... or how to farm reputation on StackOverflow
May 2021 Spark Testing ... or how to farm reputation on StackOverflow
 
Indy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleIndy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-mule
 
Test Driven Development with PHP
Test Driven Development with PHPTest Driven Development with PHP
Test Driven Development with PHP
 
Introduction of Tools for providing rich user experience in debugger
Introduction of Tools for providing rich user experience in debuggerIntroduction of Tools for providing rich user experience in debugger
Introduction of Tools for providing rich user experience in debugger
 
TDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and GomegaTDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and Gomega
 
Behaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About TestingBehaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About Testing
 
Bdd and-testing
Bdd and-testingBdd and-testing
Bdd and-testing
 
TDD - Seriously, try it! - Bucarest Tech Week
TDD - Seriously, try it! - Bucarest Tech WeekTDD - Seriously, try it! - Bucarest Tech Week
TDD - Seriously, try it! - Bucarest Tech Week
 
Deliberate Practice, New Learning Styles (2015)
Deliberate Practice, New Learning Styles (2015)Deliberate Practice, New Learning Styles (2015)
Deliberate Practice, New Learning Styles (2015)
 
Top Tips Every Notes Developer Needs To Know
Top Tips Every Notes Developer Needs To KnowTop Tips Every Notes Developer Needs To Know
Top Tips Every Notes Developer Needs To Know
 

Último

Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
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
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...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
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
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
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 

Último (20)

Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
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, ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
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
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

Working With Legacy Code

  • 1. Working with legacy code Andrea Polci
  • 2. Legacy code: definition “source code inherited from someone else and source code inherited from an older version of the software” (wikipedia) “code without tests” (Michael Feathers)
  • 3. Legacy code: caracteristics ● Poor architecture ● Stratification of modifications ● Non-uniform coding styles ● Poor written documentation ● “Oral” documentation lost ● No tests ● Extremely valuable! ● Only successful code become legacy code
  • 4. Why we have legacy code? ● More and more features ● Shortcuts and hacks ● Developer rotation ● Development team growth ● less communication
  • 5. Why we need to change legacy code? ● New functionality ● Bug ● Refactoring ● Optimization
  • 6. Options ● Start from scratch? ● Look for a new Job? ● May be we need Rambo? ● Or Mc Gyver? ● May be we need some “tools” to work effectively with legacy code
  • 7. Test ● They gives feedback ● What about legacy on changes code? ● Different kind of tests ● To modify it we need tests ● Unit ● To write test we need ● Integration to modify the code ● Functional
  • 8. An algorithm 1) Identify what to change 2) Identify what to test 3) Break dependencies 4) Write the tests 5) Modify and refactoring
  • 9. What to change 1) Identify what to change ● Do we have enough knowledge to choose where to make changes? ● Sometimes we need to modify many different places just to make a simple change. 2) Identify what to test 3) Break dependencies 4) Write the tests 5) Modify and refactoring
  • 10. I don't understand the code ● Note/Sketching ● Listing Markup ● Scratch Refactoring ● Write tests ● Delete Unused Code ● There is the repository for that
  • 11. I don't understand the structure ● Long lived applications tend to loose structure ● It takes long time to understand the big picture ● There is no big picture ● The team is in emergency mode and lose sight of the big picture ● It's important that every developer understand the big picture or the code will diverge from the architecture ● Tell the story of the system ● Naked CRC ● Conversation Scrutiny
  • 12. What to test 1) Identify what to change 2) Identify what to test • Can be an hard work • Effect analisys • How much time do we have? 3) Break dependencies 4) Write the tests 5) Modify and refactoring
  • 13. I don't have the time to test ● Be careful! ● Sometimes there is no other option ● Don't make it worse! ● Try to isolate new (tested) code from legacy code ● Sprout method/class ● Wrap method/class
  • 14. Sprout method public class ProductLablePrinter{ … public void printLabel(int productId) { String barcode; … // compute barcode … // print barcode } }
  • 15. Sprout method public class ProductLablePrinter{ … public void printLabel(int productId) { String barcode; … // compute barcode … logPrinted(barcode); // print barcode } protected void logBarcode(String barcode) { // My code } }
  • 16. Break Dependencies 1) Identify what to change 2) Identify what to test 3) Break dependencies ● How do I put a class in a test harness? ● How I know I'm not breaking anything? 4) Write the tests 5) Modify and refactoring
  • 17. Sensing & Separation ● Sensing: we break dependencies to sense when we can't access values our code computes ● Separation: we break dependencies to separate when we can't even get a piece of code into a test harness to run
  • 18. Sensing & Separation: Example public class ProductLablePrinter{ … Public void printBarcode(int productId) { String barcode; … // compute barcode … // print barcode; } }
  • 19. Seam ● Seam: a place where you can alter behavior of your program without editing in that place ● Enabling Point: Every seam has an enabling point, a place where you can make the decision to use one behaviour or another ● Looking for existings seams in legacy code allow us to break dependencies (for sensing or separation) without refactoring.
  • 20. Different kind of Seams ● Preprocessor seams ● Link seam ● Object seam
  • 21. Object seam: Example public class ProductLablePrinter{ … public void printLabel(int productId) { String barcode; … // compute barcode … printBarcode(barcode); } protected void printBarcode(String barcode) { // access to printer } } Public void testPrintBarcode() { ProductLablePrinter plp = new ProductLabelPrinter(){ String lastPrinted = null; protected void printBarcode(String barcode) {lastPrinted=barcode;} } // … test code }
  • 22. I can't get this class into a Test ● Objects of the class can't be created easily ● Parameters we have to pass to the constructor ● Hidden dependencies ● The test harness won't easily build with the class in it ● The constructor we need to use has bad side effects ● Significant work happens in the constructor and we need to sense it
  • 23. Example (1) public void testLabelPrinter() { new LabelPrinter(); } The constructor LabelPrinter is undefined
  • 24. Example (2) public class LabelPrinter { public LabelPrinter(Printer prn, Warehose wh) { … this.printer = prn; if(!this.printer.isOnline()) { throw new … } } } public void testLabelPrinter() { Printer prn = new Printer(“stampante”); Warehouse wh = new Warehouse(“magazzino1”); LabelPrinter labPrint = new LabelPrinter(prn, wh); } public class Printer { boolean isOnline(){ … } void startJob() { … } void printLine(String line) { … } void endJob() { … } }
  • 25. Example (3) public interface PrinterInterface { boolean isOnline(); void startJob(); void printLine(String line); void endJob(); } public class Printer implements PrinterInterface { … } public class LabelPrinter { public LabelPrinter(PrinterInterface prn, Warehose wh) { ... } }
  • 26. I can't run this method in a test ● Method not accessible to the test ● It's hard to construct the parameters ● Side effects (database, access to hardware, ecc.) ● Need to sense through objects used by the method
  • 27. Test 1) Identify what to change 2) Identify what to test 3) Break dependencies 4) Write the tests 5) Modify and refactoring
  • 28. Modify and refactoring 1) Identify what to change 2) Identify what to test 3) Break dependencies 4) Write the tests 5) Modify and refactoring ● TDD ● Programming by difference
  • 29. Making changes takes too much time ● Understanding ● How can we increase our understanding of the code? ● Lag Time ● It takes to much time to see the effect of changes and this slow down the development ● Built time ● Slow tests ● No unit tests ● Often to solve this we need to break dependecies
  • 30. Tools ● Authomatic refactoring tools ● Can we trust them? ● Mock Objects ● Unit test harnesses ● JUnit ● Other test harnesses ● Non-unit tests
  • 31. Conclusions ● No “silver bullet” here ● It's an hard work but (usually) not impossible ● At first it will seems overwhelming, but things will get better as the number of tests increase ● Be pragmatic!
  • 33. References ● Workking Effectively with Legacy Code, Michael C. Feathers ● Joel on Software: Things you should never do, part I http://www.joelonsoftware.com/articles/fog0000000069.html ● Michael Dubakov: Refactoring vs Rewrite http://www.targetprocess.com/blog/2009/11/refactoring-vs-rewrite.html