3 ms·
> If you have a 4 line stanza that needs a comment to explain what it's doing then I'd suggest that needs breaking out into a function I'm mostly in agreement,
by fishtoaster 4y ago
> If you have a 4 line stanza that needs a comment to explain what it's doing then I'd suggest that needs breaking out into a function
I'm mostly in agreement, so I'm going to focus on my disagreement too! ;)
This view is pretty common in the ruby community. So much so that I think they take it too far at times. I sometimes find myself reading a class that could have been a single 24-line method but is instead one method with method calls, each of which calls 0-2 more methods.
This style is good for when I'm trying to quickly understand what that class is supposed to do - I just read the top-level method that looks like:
thing = getThing()
munge { |i| thing[i] }
is_foobar?(thing)
But it's terrible when I need to understand what that function is actually doing - eg to debug it or to find some underlying function call I'm looking for. I have to bounce all over the file to mentally reconstruct a linear sequence of code which could have just been one function with some headers.
Of course, this style is a reaction to impenetrable 500-line functions which are also terrible. I'd definitely prefer many small functions to that! I think it's a matter of judgement and experience to know whether some code is better as small stanzas with comments or small functions.