by eru · 11y ago · 0 comments
Functions communicate with the outside world only via arguments and returned values. The explicit split reassures the reader that this is indeed the case, and thus every reader doesn't have to establish this fact via careful reasoning again and again.
A long function might indeed be simple, but you never know until you check carefully.
(This only applies to pure functions. Natural language sentences and side effecting procedures are messier.)
It is a bit dated now, but Code Complete cites actual attempts to study the connections between function length and maintenance cost in real world code bases. The conclusion was that functions with a high internal complexity were maintenance hazards. Basically add up all of the if's, loops, and so on, and if that is a big number, then break it up. However long functions without internal complexity were not a maintenance hazard. This was true up to about 200 lines.
OK, a lot has changed since IBM studied maintenance costs in the 1980s. But the received wisdom today about short functions was already the received wisdom then. And the wisdom today is just as much based on opinion as it was then.
Is there any more reason why we should accept the received wisdom today than there was then?
My argument was actually mostly from a Haskell point of view, and a study there would be somewhat harder to define: you are defining functions all the time: especially in imperative code (do-block / monads) each line is actually a callback.
You could go by purely syntactical criteria, though. Culturally, even a single screenful of code is considered rather long for one definition in Haskell.
this style really makes you wish C/C++ had if expressions. you end up using a lot of ternary operators to compensate.
it makes the code a bit more verbose, but its worlds better than poring through a function to make sure nothing is mutated in an unexpected way.
I don't know my way around compiler internals, but this sounds a little suspect to me. It seems to me—just a tyro's opinion—that (1) work spent doing what the compiler will do anyway is probably work wasted, and (2) it is possible to get in the compiler's way even if you are trying just to do what it would do anyway, thus causing unexpected degradation.
This is actually compatible with DRY, because overpartitioned functionality will require more state to be pulled from function to function, resulting in repeating, long argument lists.
Yes, for some compilers and interpreters performance considerations will force you to write less than readable code.
I think that that article describes a reaction against a too-hasty move towards microservices, and (as someone who's not a businessperson myself!) as such it seems well advised; but I suspect that there is, among programmers at large, no too-hasty move towards 'microfunctions', and so no corresponding need to urge people to make the kind of monolithic monstrosities that novice (and even sometimes experienced) programmers are all too willing to make anyway.