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/

53b37e14a09c5a718a39fda61fe1b8e5?s=128

PyCon 2019

May 05, 2019
Tweet

Transcript

  1. None
  2. None
  3. None
  4. None
  5. None
  6. Why refactor?

  7. Why refactor?

  8. None
  9. None
  10. None
  11. None
  12. None
  13. None
  14. None
  15. None
  16. None
  17. Common Code Smell #1 It’s too long!

  18. • • • ◦ ◦

  19. None
  20. • •

  21. • •

  22. None
  23. Code Smells: Long methods • • • ◦ ◦ ◦

  24. Tool: Replace Method with Method Object

  25. Tool: Replace Method with Method Object

  26. Tool: Introduce a parameter object

  27. • • • ◦

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

  29. • • • ◦ ◦ ◦

  30. None
  31. None
  32. • • • ◦ ◦

  33. Common Code Smell #3: OOP issues

  34. Code smell: Divergent change • • • ◦ ◦

  35. Code smell: Shotgun surgery • • • ◦ ◦

  36. Code smell: shotgun surgery

  37. Tool: Move Class

  38. Code smell: Parallel inheritance hierarchies • ◦ • • ◦

  39. Code Smell: Parallel inheritance hierarchies

  40. Tool: Move Class

  41. • • • ◦ ◦

  42. • • • ◦ ◦

  43. Tool: Extract Class

  44. Tool: Extract Class

  45. • • •

  46. Tool: Replace Type Code with State

  47. • • • ◦ ◦

  48. Tool: Hide Delegate

  49. Tool: Hide Delegate

  50. • • • ◦

  51. • • • ◦

  52. Do I need to inherit this class?

  53. None
  54. Lets talk Pythonic Code Smells!

  55. • • • ◦ ◦ ◦

  56. • • • ◦ ◦

  57. Codesmell: Unnecessary boilerplate code

  58. Dataclasses and attrs

  59. Dataclasses and attrs

  60. • • • ◦

  61. • • • ◦

  62. None
  63. None
  64. None
  65. None
  66. None
  67. None
  68. None
  69. None
  70. None
  71. None
  72. None
  73. None
  74. None
  75. None
  76. None
  77. None
  78. None
  79. None
  80. None
  81. None
  82. None
  83. None
  84. • ◦ • ◦

  85. • • ◦ ◦ ◦

  86. • •

  87. • •

  88. • •

  89. None
  90. None
  91. • (Expand) (Contract) (Transition)

  92. • •

  93. None
  94. None
  95. None
  96. None
  97. None
  98. None
  99. None
  100. None
  101. None
  102. None
  103. None
  104. None
  105. None
  106. None
  107. THANK YOU! @amandasopkin

  108. 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