refactoring long methods
DESCRIPTION
Presented at Philippine Ruby Users Group Meetup last January 23, 2014TRANSCRIPT
![Page 1: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/1.jpg)
![Page 2: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/2.jpg)
![Page 3: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/3.jpg)
![Page 4: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/4.jpg)
![Page 5: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/5.jpg)
Marc Ignaciotwitter: @pd_igithub: @padi
![Page 6: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/6.jpg)
Refactoring
![Page 7: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/7.jpg)
![Page 8: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/8.jpg)
RefactoringLong Methods
![Page 9: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/9.jpg)
Definitions
![Page 10: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/10.jpg)
Refactoring
![Page 11: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/11.jpg)
● “…is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.”
- refactoring.com
Refactoring
![Page 12: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/12.jpg)
● “…is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.”
- refactoring.com
Refactoring
![Page 13: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/13.jpg)
● “…is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.”
- refactoring.com
Refactoring
![Page 14: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/14.jpg)
● “…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
Refactoring
![Page 15: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/15.jpg)
● “…is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.”
- refactoring.com
Refactoring
![Page 16: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/16.jpg)
a technique for restructuring code without changing
behavior
Refactoring
![Page 17: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/17.jpg)
a technique for making code suck less without changing
behavior
Refactoring
![Page 18: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/18.jpg)
Long Methods
![Page 19: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/19.jpg)
● “a method, function, or procedure that has grown too large”
Long Methods
![Page 20: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/20.jpg)
● “a method, function, or procedure that has grown too large”
Long Methods
![Page 21: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/21.jpg)
● “a method, function, or procedure that has grown too large”
● IMO: a method with a flog score of > 20
Long Methods
![Page 22: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/22.jpg)
● 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.”
Flog
![Page 23: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/23.jpg)
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
![Page 24: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/24.jpg)
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
![Page 25: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/25.jpg)
Techniques
![Page 26: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/26.jpg)
● Extract Method● Inline Method● Inline Temp● Replace Temp with Query● Replace Method w/ Method Object
Techniques
![Page 27: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/27.jpg)
Examples
![Page 28: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/28.jpg)
Resources
![Page 29: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/29.jpg)
![Page 30: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/30.jpg)
![Page 31: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/31.jpg)
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
kk
![Page 32: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/32.jpg)
Questions?
![Page 33: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/33.jpg)
Thanks!
![Page 34: Refactoring Long Methods](https://reader034.vdocuments.us/reader034/viewer/2022052619/5557ac5ed8b42a696c8b4a9d/html5/thumbnails/34.jpg)