Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Refactoring Long Methods

706 views

Published on

Presented at Philippine Ruby Users Group Meetup last January 23, 2014

Published in: Career, Technology, Design
  • Be the first to comment

  • Be the first to like this

Refactoring Long Methods

  1. 1. Marc Ignacio twitter: @pd_i github: @padi
  2. 2. Refactoring
  3. 3. Refactoring Long Methods
  4. 4. Definitions
  5. 5. Refactoring
  6. 6. Refactoring ● “…is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.” - refactoring.com
  7. 7. Refactoring ● “…is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.” - refactoring.com
  8. 8. Refactoring ● “…is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.” - refactoring.com
  9. 9. Refactoring ● “…is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.” - refactoring.com ● “Tests are implied.” - Katrina Owen
  10. 10. Refactoring ● “…is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.” - refactoring.com
  11. 11. Refactoring a technique for restructuring code without changing behavior
  12. 12. Refactoring a technique for making code suck less without changing behavior
  13. 13. Long Methods
  14. 14. Long Methods ● “a method, function, or procedure that has grown too large”
  15. 15. Long Methods ● “a method, function, or procedure that has grown too large”
  16. 16. Long Methods ● “a method, function, or procedure that has grown too large” ● IMO: a method with a flog score of > 20
  17. 17. Flog ● ruby gem for analyzing code pain ● “Flog reports the most tortured code in an easy to read pain report.” ● “The higher the score, the more pain the code is in.”
  18. 18. Flog Scores ● ● ● ● ● ● ● 0-10 Awesome 11-20 Good enough 21-40 Might need refactoring 41-60 Possible to justify 61-100 Danger 100-200 Whoop, whoop, whoop 200 + Someone please think of the children
  19. 19. Long Methods ● If a method is long, it’s likely that it does too many things. ● “Methods, like classes, should have single responsibility.” - Sandi Metz ● SRP - every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class
  20. 20. Techniques
  21. 21. Techniques ● ● ● ● ● Extract Method Inline Method Inline Temp Replace Temp with Query Replace Method w/ Method Object
  22. 22. Examples
  23. 23. Resources
  24. 24. Resources - "Refactoring: Improving the Design of Existing Code", Martin Fowler - refactoring.com - jakescruggs.blogspot.com/2008/08/ whats-good-flog-score.html Code: - "Refactoring, Ruby Edition", Martin Fowler et al. - "Therapeutic Refactoring", Katrina Owens
  25. 25. Questions?
  26. 26. Thanks!

×