34

Refactoring Long Methods

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Refactoring Long Methods
Page 2: Refactoring Long Methods
Page 3: Refactoring Long Methods
Page 4: Refactoring Long Methods
Page 5: Refactoring Long Methods

Marc Ignaciotwitter: @pd_igithub: @padi

Page 6: Refactoring Long Methods

Refactoring

Page 7: Refactoring Long Methods
Page 8: Refactoring Long Methods

RefactoringLong Methods

Page 9: Refactoring Long Methods

Definitions

Page 10: Refactoring Long Methods

Refactoring

Page 11: Refactoring Long Methods

● “…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

● “…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

● “…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

● “…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

● “…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

a technique for restructuring code without changing

behavior

Refactoring

Page 17: Refactoring Long Methods

a technique for making code suck less without changing

behavior

Refactoring

Page 18: Refactoring Long Methods

Long Methods

Page 19: Refactoring Long Methods

● “a method, function, or procedure that has grown too large”

Long Methods

Page 20: Refactoring Long Methods

● “a method, function, or procedure that has grown too large”

Long Methods

Page 21: Refactoring Long Methods

● “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

● 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

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

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

Techniques

Page 26: Refactoring Long Methods

● Extract Method● Inline Method● Inline Temp● Replace Temp with Query● Replace Method w/ Method Object

Techniques

Page 27: Refactoring Long Methods

Examples

Page 28: Refactoring Long Methods

Resources

Page 29: Refactoring Long Methods
Page 30: Refactoring Long Methods
Page 31: Refactoring Long Methods

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

Questions?

Page 33: Refactoring Long Methods

Thanks!

Page 34: Refactoring Long Methods