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

Amanda Sopkin - The Refactoring Balance Beam: When to Make Changes and When to Leave it Alone

Amanda Sopkin - The Refactoring Balance Beam: When to Make Changes and When to Leave it Alone

Many developers struggle to find the balance between striving to improve existing code and letting good enough alone by accepting certain shortcomings. As a new developer to a team it can be difficult to understand existing strategies and patterns that are sometimes flat out bad (and often openly acknowledged as such). Often the result of tight deadlines or unclear specifications, even the best developers write code they later look back upon with shudders. So how do we decide when refactoring is worth it? Come learn strategies for refactoring with minimal impact, methods for working with bad code you can’t change, and strategies for knowing the difference between what is fixable and what is better left alone.

https://us.pycon.org/2019/schedule/presentation/213/

PyCon 2019

May 05, 2019
Tweet

More Decks by PyCon 2019

Other Decks in Programming

Transcript

  1. Why refactor?

    View full-size slide

  2. Why refactor?

    View full-size slide

  3. Common Code Smell #1
    It’s too long!

    View full-size slide






  4. View full-size slide

  5. Code Smells: Long methods







    View full-size slide

  6. Tool: Replace Method with Method Object

    View full-size slide

  7. Tool: Replace Method with Method Object

    View full-size slide

  8. Tool: Introduce a parameter object

    View full-size slide





  9. View full-size slide

  10. Common Code Smell #2:
    It’s not useful enough!

    View full-size slide







  11. View full-size slide






  12. View full-size slide

  13. Common Code Smell #3:
    OOP issues

    View full-size slide

  14. Code smell: Divergent change





    View full-size slide

  15. Code smell: Shotgun surgery





    View full-size slide

  16. Code smell: shotgun surgery

    View full-size slide

  17. Tool: Move Class

    View full-size slide

  18. Code smell: Parallel inheritance hierarchies






    View full-size slide

  19. Code Smell: Parallel inheritance hierarchies

    View full-size slide

  20. Tool: Move Class

    View full-size slide






  21. View full-size slide






  22. View full-size slide

  23. Tool: Extract Class

    View full-size slide

  24. Tool: Extract Class

    View full-size slide

  25. Tool: Replace Type Code with State

    View full-size slide






  26. View full-size slide

  27. Tool: Hide Delegate

    View full-size slide

  28. Tool: Hide Delegate

    View full-size slide





  29. View full-size slide





  30. View full-size slide

  31. Do I need to inherit this class?

    View full-size slide

  32. Lets talk Pythonic Code Smells!

    View full-size slide







  33. View full-size slide






  34. View full-size slide

  35. Codesmell: Unnecessary boilerplate code

    View full-size slide

  36. Dataclasses and attrs

    View full-size slide

  37. Dataclasses and attrs

    View full-size slide





  38. View full-size slide





  39. View full-size slide





  40. View full-size slide






  41. View full-size slide


  42. (Expand) (Contract)
    (Transition)

    View full-size slide

  43. THANK YOU!
    @amandasopkin

    View full-size slide

  44. Sources
    Flat Icon: https://www.flaticon.com/
    Refactoring Python by Brett Slatkin: https://www.youtube.com/watch?v=D_6ybDcU5gc
    Real Python: https://realpython.com/python-refactoring/
    Pep 20: https://www.python.org/dev/peps/pep-0020/
    Beyond Pep 8 by Raymond Hettinger: https://www.youtube.com/watch?v=wf-BqAjZb8M
    Refactoring by Martin Fowler:
    https://www.csie.ntu.edu.tw/~r95004/Refactoring_improving_the_design_of_existing_code.pdf

    View full-size slide