Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Refactoring legacy code using Approval testing (Meetup Alicante)

Refactoring legacy code using Approval testing (Meetup Alicante)

Slides used during the Software Crafters Meetup session.
The session had two parts:
* An introduction and open discussion about concepts like legacy code and technical debt.
* A live-coding session to show a technique called "approval testing", applying it to cover with tests some "legacy code".

https://www.meetup.com/es-ES/Software-Crafters-Alicante/events/267849996/

Isidro López

January 22, 2020
Tweet

More Decks by Isidro López

Other Decks in Programming

Transcript

  1. Refactoring legacy code using
    approval testing
    Meetup SW Crafters Alicante, 22.01.2020
    @islomar

    View Slide

  2. Ego slide
    Hemos venido a jugar...
    Desarrollador de software/producto
    @islomar
    [email protected]

    Teleco, Antropología Social y
    Cultural, Executive MBA, CSM…

    View Slide

  3. What is legacy code for you?

    View Slide

  4. For some people, legacy code is about...

    Code not developed by me

    Code that you don't know

    Code without tests

    Code difficult to maintain (difficult to understand or change)

    Code no longer supported

    Code that you would prefer not to touch

    ?

    View Slide

  5. What is technical debt for you?

    View Slide

  6. Is technical debt always "bad"?

    View Slide

  7. Debt metaphor by Ward Cunningham (1992)
    “Shipping first time code is like going into debt. A little debt
    speeds development so long as it is paid back promptly with a
    rewrite.
    [...] The danger occurs when the debt is not repaid.”

    View Slide

  8. About technical debt

    The extra effort that it takes to keep on working on the code
    is the interest paid on the debt.

    It slows you down in baby steps, not realizing and taking for
    granted that “it is the way it is”.

    OK in isolated and stable areas which are “never” touched.

    View Slide

  9. Technical Debt Quadrant
    https://martinfowler.com/bliki/TechnicalDebtQuadrant.html

    View Slide

  10. How to refactor (complex) legacy code?

    We do not have tests as safety net before starting to refactor!

    But… what if it is not really clear what is the current behavior?

    View Slide

  11. Approval testing to the rescue!!
    It also exists something called Golden Master
    "Approval testing is a test technique which compares the current output of your code
    with an "approved" version. The approved version is created by initially examining the
    test output and approving the result."
    Assume the current behaviour is correct: the first goal is to refactor in a safe way!!

    View Slide

  12. View Slide

  13. Live Coding time!!
    https://github.com/islomar/approval-testing

    View Slide

  14. Resources: kata and approval testing
    ● Kata
    ○ Description of the kata
    ○ https://github.com/emilybache/GildedRose-Refactoring-Kata
    ○ https://github.com/emilybache/GildedRose-Approval-Kata
    ○ http://coding-is-like-cooking.info/2013/03/writing-good-tests-for-the-gilded
    -rose-kata/
    ● Approval testing
    ○ https://www.infoq.com/news/2017/02/approval-testing-texttest
    ○ http://approvaltests.com/

    View Slide

  15. Resources: screencasts and books
    ● Screencasts
    ○ Refactoring legacy code with approval testing (Emily Bache)
    ○ Approval tests with Lift Kata (Emily Bache)
    ○ Testing and refactoring legacy code (Sandro Mancuso)
    ○ Approval tests with Java
    ● Emily Bache
    ○ https://www.praqma.com/stories/all/#emilybache
    ○ https://github.com/emilybache
    ○ How to develop new features with approval testing

    View Slide

  16. Resources: refactoring and technical debt

    Original writing about technical debt from Ward Cunningham

    The Debt Metaphor (video)

    Tech Debt - MPJ's Musings - Fun Fun Function
    ● https://martinfowler.com/bliki/TechnicalDebt.html
    ● https://martinfowler.com/bliki/TechnicalDebtQuadrant.html
    ● https://www.fluentcpp.com/2020/01/17/technical-debt-is-like-a-tetris-game/
    ● https://martinfowler.com/bliki/DesignPayoffLine.html
    ● https://martinfowler.com/bliki/DesignStaminaHypothesis.html
    ● https://martinfowler.com/articles/workflowsOfRefactoring/
    ● https://www.praqma.com/stories/advanced-testing-refactoring-techniques/

    View Slide

  17. View Slide

  18. View Slide

  19. View Slide