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

Theatrical players - refactoring Kata

Yoan
October 16, 2019

Theatrical players - refactoring Kata

A refactoring kata done in several teams to understand Approval tests and how to refactor legacy code with :
- OOP paradigms
- FP paradigms

Yoan

October 16, 2019
Tweet

More Decks by Yoan

Other Decks in Programming

Transcript

  1. @algrison
    @yot88
    Theatrical players
    refactoring Kata

    View Slide

  2. @algrison
    @yot88
    Clone
    https://github.com/emilybache/Theatrical-Players-
    Refactoring-Kata

    View Slide

  3. @algrison
    @yot88
    Objective
    • Learn how to refactor “legacy” code
    • Practice FP concepts
    Theatrical players refactoring Kata
    Exercise
    • Add an HTML output with the same information

    View Slide

  4. Will it be easy to do the exercise ?
    Why ?
    What do you think about this code ?
    https://github.com/emilybache/Theatrical-Players-Refactoring-Kata

    View Slide

  5. 40 lines

    View Slide

  6. Formatting
    Amount calculation
    Credits calculation

    View Slide

  7. Formatting
    Amount calculation
    Credits calculation
    BROKEN

    View Slide

  8. @algrison
    @yot88
    How to start ?

    View Slide

  9. View Slide

  10. @algrison
    @yot88
    Which kind of tests ?
    The code already exists and works :
    • Easiest way to add a regression test is to find some test data, exercise the code,
    and approve the result
    • Add approval tests / snapshot tests / Golden master

    View Slide

  11. @algrison
    @yot88
    Approval tests : Generate output
    Statement for BigCo
    Hamlet: $650.00 (55 seats)
    As You Like It: $580.00 (35 seats)
    Othello: $500.00 (40 seats)
    Amount owed is $1,730.00
    You earned 47 credits

    View Slide

  12. @algrison
    @yot88
    Approval tests :
    Create a test
    https://approvaltests.com/
    Statement for BigCo
    Hamlet: $650.00 (55 seats)
    As You Like It: $580.00 (35 seats)
    Othello: $500.00 (40 seats)
    Amount owed is $1,730.00
    You earned 47 credits
    StatementPrinterTests.exampleStatem
    ent.approved.txt

    View Slide

  13. @algrison
    @yot88
    Approval tests :
    Lines not covered

    View Slide

  14. @algrison
    @yot88
    Confidence in the tests
    Mutation testing

    View Slide

  15. @algrison
    @yot88
    Let’s refactor
    https://github.com/emilybache/Theatrical-Players-Refactoring-Kata

    View Slide

  16. @algrison
    @yot88
    Step by step (OO style)
    1) Extract the amount calculation
    • For example you can use the Strategy pattern
    o Tragedy & Comedy
    • You can also use some kind of factory to retrieve the correct impl.
    2) Extract the credits calculation
    3) Create a specific Printer implementation
    • print(Line)
    • print(Statement)

    View Slide

  17. @algrison
    @yot88
    1) Extract the amount calculation

    View Slide

  18. @algrison
    @yot88
    2) Extract the credits calculation

    View Slide

  19. @algrison
    @yot88
    3) Create a specific Printer implementation

    View Slide

  20. @algrison
    @yot88
    Putting whole together

    View Slide

  21. @algrison
    @yot88
    Step by step (FP style)
    1) Extract the amount calculation
    • Create dedicated functions for both types
    • Tragedy & Comedy
    • Build a map of these implementations
    2) Build a statement
    3) Run the pipeline
    • Iterate
    • Reduce
    • Format

    View Slide

  22. @algrison
    @yot88
    1) Extract the amount calculation

    View Slide

  23. @algrison
    @yot88
    2) Build a statement

    View Slide

  24. @algrison
    @yot88
    3) Run the pipeline

    View Slide

  25. @algrison
    @yot88
    Resources
    • https://www.praqma.com/stories/refactoring-kata/
    • https://github.com/emilybache/Theatrical-Players-Refactoring-Kata
    • https://approvaltests.com/
    • Strategy pattern
    • https://refactoring.guru/design-patterns/strategy

    View Slide