r/ruby • u/gregbaugues • Aug 18 '17
Small Functions Considered Harmful
https://medium.com/@copyconstruct/small-functions-considered-harmful-91035d316c297
u/midasgoldentouch Aug 18 '17
I honestly think that could have benefited from some code examples. Maybe I wasn't concentrating hard enough, but I think her points would be more convicting with actual examples. I agree with some of the points, but I think code examples could have made the arguments that much stronger.
4
u/pducks32 Aug 18 '17
Just on the topic alone, I don’t agree. Sure I guess having a single line function is not always necessary.
‘‘‘ def this_thing object.thing end ‘‘‘
Could be done as ‘this_thing = object.method‘ but many times just throwing in an extra method to clear something up really really saves other devs time. I’m not big on commenting my code because I try and make my variable names and methods explain comment themselves. If I have to have a method name that’s 30 characters long then I will.
9
u/zaphnod Aug 18 '17 edited Jul 01 '23
I came for community, I left due to greed
1
u/pducks32 Aug 19 '17
Yea great point. I’ve never heard newbies to my company's code complain that a file is 200 lines (there’s a ton of room to refactor but that’s a different story). I’ve only ever heard them complain about the class with the 50 line method of weird symbols and unintelligible variable names.
4
7
u/herminator Aug 18 '17
Jesus, that's terribly written. It starts off as if the author just had a class on writing essays, then finishes as if they had a stroke. The intro is so formulaic, it's cringe-worthy, while he whole section at the end about "When do smaller functions actually make sense" makes almost no sense, and hardly goes into the matter at all.
2
1
u/remain_calm Aug 19 '17
The authors assertion that many small functions make the code harder to change is mind boggling. It is the exact opposite of what I've observed in over a decade of professional development.
1
u/azuretan Aug 19 '17
Small functions are harmful. Large functions are harmful. What’s next, all functions are harmful?
1
u/Sqeaky Aug 19 '17
Had this discussion on Hacker News like a week ago. Pretty much only fools think small functions are harmful, fools on the order of "gotos are good".
There is a time and place for both large functions and gotos, but they are so infrequent that one should consider the other options carefully.
-5
u/artpop Aug 18 '17
The small function thing could be sustainable if Ruby was statically typed, but its not. So the author of this is 100% right IMO.
Good luck having the argument with the team about changing the Rubocop rules though. Ruby developers are mighty cultish.
So instead, leave that team. Start writing statically typed functional programming in a different language. Learn to love IDEs and generally find happiness (and performance) again, elsewhere.
5
u/andyw8 Aug 18 '17
I don't understand why using small functions would be related to whether a language is statically typed or not?
1
u/artpop Aug 18 '17
They make even less sense when looking at them in isolation if you don’t know the types of things.
1
u/rapidsight Aug 19 '17
Always notate your methods return values, AT LEAST.
@return [Array<String>] def it_all %w[please for the love of humanity] end
15
u/human_tendencies Aug 18 '17
It's a pet-peeve of mine when an author titles their blog post "XYZ Considered Harmful." The implication is that the industry has largely adopted such a viewpoint. But really, most of these authors are just stating their personal preferences.
I wish instead they'd say something like "Why I Dislike Small Functions". I'd still read it, and it's much more honest.
NOTE: This is a criticism of the article's actual author, not so much OP.