r/ruby • u/moomaka • Aug 11 '17
Small Functions considered Harmful
https://medium.com/@cindysridharan/small-functions-considered-harmful-91035d316c294
u/DudeManFoo Aug 13 '17
Yeah... still not buying it... This kind of article says... yea... small functions and DRY code are good in 99% of cases... but be on the lookout for that all important 1% where it is not...
I find it a stretch of Sandi Metz talking about duplication being better than the wrong abstraction...
I think the point Sandi was trying to make is that a little duplication is ok while you are initially solving the problem, but then the 'design' gets better because your testing allows you to make code that is easier to 'change'... and in order to change it... it has to be easier to understand.
Platitudes like 'use common sense' don't really help someone new at code tackle complex problems... TDD / BDD / Small Functions / DRY are things people can grasp quickly and use their entire coding lifetime... 'use common sense' is about as productive as 'work hard'... or 'be attractive'... or 'don't be unattractive'
1
u/moomaka Aug 19 '17
Yeah... still not buying it... This kind of article says... yea... small functions and DRY code are good in 99% of cases... but be on the lookout for that all important 1% where it is not...
My experience is the reverse, small functions and DRY are great only 1% of the time, when the design happens to be 'good' for the future.
Platitudes like 'use common sense' don't really help someone new at code tackle complex problems... TDD / BDD / Small Functions / DRY are things people can grasp quickly and use their entire coding lifetime...
Teaching bad programmers to write short functions just results in more bad code. I'd rather refactor a 500 line method than 500 bad abstractions.
1
u/DudeManFoo Aug 19 '17
Sure, I write 500 line functions sometimes... I'll even make them 500 characters wide sometimes... and use whitespace that makes other people feel all funny... I am not a zealot about testing 100% of the time either...
As someone who has dug thru 300K lines of duplicate (and very bad) PERL code... I just can't say how much the DRY concept has helped me... and as all libraries are about DRY, I can't imagine using an OS that was not based on it... (even with the DLL hell that windows has become, and the mess that GEMs can be or all the goofy Linux libs out there...)
I don't think teaching them small functions is the point of small functions as much as a more understandable namespace and less for the new guy to keep in his working memory at one time....
But if it works for you, full steam ahead... ;)
11
u/realntl Aug 11 '17
Tautology incoming: The right length for the method is the right length for the method.
Rather than setting rules or goals for LoC per method, it's best to consider each method independently. Methods that "do one thing well" might be 20 lines long, under some circumstances. Conversely, I've seen some two line methods are handling two entirely distinct concerns and suggests the design may need to be reworked.
4
u/jrochkind Aug 11 '17
yup. You can use rubocop rules as a guide if you like, but slavishly following them is neither necessary nor sufficient for good code. I am not a fan of the cult of rubocop.
1
u/Blimey85 Aug 11 '17
Rubocop helped me change some stuff that worked but could be refactored to be a bit better. I don't follow the length guides strictly though.
5
u/jrochkind Aug 12 '17 edited Aug 12 '17
In my experience, the issue is that method line length or algorithmic complexity factors do usually point out code that could be improved by refactoring.
But you can refactor to make the metrics happy while actually making the code worse, just by sort of robotically extracting methods and classes. Arriving at the sort of thing mentioned in the OP.
Or you can spend a lot more time really thinking it through, thinking about your developer use cases to really get the right abstraction, and meet metrics with an architectural improvement.
The second one is hard, and as the OP makes takes domain knowledge you might not have yet, and even if it's right for "now" may make refactoring later harder if you didn't get the abstractions right. And it might just not be worth it even if you do manage to come up with something better that will stand the test of time. The previous code might have been good enough despite failing rubocop or other metrics (or might not be, you've got to use your human brain).
So the "correct" decision might be "Yeah, it could be made better, if I was sure what the right abstractions were, but it's good enough for now, it's not disastrous code."
But human nature is to prefer the easy way out, and to prefer not to mark a code as ignored-by-this-rubocop-test, cause that feels like an admission of defeat.
I think the only corrective is establishing the shared understanding that rubocop is a guide, and that just meeting rubocop metrics doens't neccesarily result in good code/architecture, and can actually make it worse if you are just programming to the metric.
(It actually just occurred to me that this is another example of that principle where people working to metrics effects results and makes the metrics no longer valid measurements. I forget and can't find the name of that principle, anyone? A more common example is with educational testing).
-3
u/realntl Aug 11 '17
Agreed. I've never found rubocop useful. In my experience, practices and habits are best disseminated through face to face collaboration.
2
u/morphemass Aug 12 '17
A good read.
From experience I would say that they trend to small functions is a symptom of a poor understanding (or lazy application) of OOD. I've seen many programmers with an inability to recognise when a set of functions are cohesive and provide a valuable abstraction, or that they are indicative of a domain type - indeed small functions and primitive obsession tend to go hand in hand.
4
u/JustinCampbell Aug 11 '17
If this article was shorter, I would have had an easier time understanding its points.
3
2
1
u/jdickey Aug 12 '17
This reminds me of why, in areas generally outside tech, I've long called myself an "anti-extremist extremist". Too easy to fall into extremes of slavishly following the tools, though. RuboCop and ESlint have easily cost me two staff-weeks this year alone.
12
u/NoahTheDuke Aug 11 '17
"Considered Harmful" Essays Considered Harmful