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

9 ways to test your spaghetti code

9 ways to test your spaghetti code

“Test the legacy code as well” has been a mantra for many years now. But how do you actually do that? When stuck with tangled legacy-spaghetti, it may be hard to see the way out. The path from struggling with your spaghetti into doing TDD is shorter than you think.

It's so easy to say that you should test code as you change it, now matter how legacy, but in a real-world project, you need to know some tools and techniques to be able to do that. This talk will give you concrete techniques, along with a way of thinking to figure out those tricks you need in your code base.

I came into a project where writing tests was considered really hard and time consuming, and TDD was “impossible”. I did it anyway, and I want to share what we did and what we've learned from it.

Given at https://javaday.istanbul/

Mads Opheim

March 16, 2019
Tweet

More Decks by Mads Opheim

Other Decks in Technology

Transcript

  1. JavaDay Istanbul
    9 ways to test your
    spaghetti code
    Mads Opheim
    @MadsOpheim

    View Slide

  2. Goal of this talk:
    You’ll be enabled to test
    your spaghetti code
    2

    View Slide

  3. What is
    spaghetti
    code?
    3

    View Slide

  4. I don’t like testing
    4

    View Slide

  5. I don’t like testing
    manual verification
    5

    View Slide

  6. Precondition:
    You are creative
    The computer is not
    6

    View Slide

  7. Don’t
    Repeat
    Yourself
    7

    View Slide

  8. The 9 ways
    8

    View Slide

  9. 9
    1. Test all
    or
    Test small

    View Slide

  10. 10
    2. Set your
    dependencies

    View Slide

  11. 11

    View Slide

  12. Use interfaces
    12

    View Slide

  13. 13

    View Slide

  14. Singletons
    14

    View Slide

  15. 3. Package-protect
    problematic parts
    15

    View Slide

  16. 16

    View Slide

  17. 4. Consider removing final
    or static
    17

    View Slide

  18. In general:
    untangling
    18

    View Slide

  19. You’re not as smart
    as you think
    19

    View Slide

  20. Well-designed code is
    testable code
    20

    View Slide

  21. Your test code should be
    simple
    21

    View Slide

  22. Do simple refactorings to
    get your code under test
    22

    View Slide

  23. Use
    characterization
    tests
    23

    View Slide

  24. 24
    5. Help your team

    View Slide

  25. 6. Run your tests - and care
    25

    View Slide

  26. 7. Feature toggles
    26

    View Slide

  27. 8. One class can have
    several test classes
    27

    View Slide

  28. One mile at a time
    28

    View Slide

  29. Refactor in separate
    commits
    29

    View Slide

  30. 9. Test-driven
    development
    30

    View Slide

  31. Test-driven
    spaghetti
    31

    View Slide

  32. TDD on legacy code in
    practice
    32

    View Slide

  33. Get started with TDD
    33

    View Slide

  34. Tip 10, 11 and onwards
    34

    View Slide

  35. Beware of limiting
    beliefs
    35

    View Slide

  36. You’ll forget things
    36

    View Slide

  37. You’ll do stupid
    things - and that’s ok
    37

    View Slide

  38. You get what you
    measure
    38

    View Slide

  39. Real or fake data?
    39

    View Slide

  40. Be consistent
    40

    View Slide

  41. Give me
    more
    @lisacrispin,
    @lisihocke,
    @techgirl1908,
    @maaretp...
    41

    View Slide

  42. Bonustrack:
    The technical domain?
    42

    View Slide

  43. Split technical tests
    from functional
    tests
    43

    View Slide

  44. 44
    The Deadline For
    Kunngjøring Is Four Weeks()

    View Slide

  45. 45
    Properties For Namsmann
    Mainly Follow The Same
    Rules as Hovedstevnevitne()

    View Slide

  46. Key takeaways
    46
    1. Good code design improves testability
    2. TDD on spaghetti code: TDD + test
    spaghetti code
    3. Write tests for you legacy code -
    you can do it!

    View Slide

  47. Thank you!
    47
    @MadsOpheim
    [email protected]

    View Slide