3 ms·
I guess the real problem is that writing good short functions is hard, so some people might find it better to just write long functions. By good short functions
by slx26 6y ago
I guess the real problem is that writing good short functions is hard, so some people might find it better to just write long functions. By good short functions I mean functions that, as you say, don't mix levels of abstraction, use proper names, and follow a consistent pattern so their behavior can be safely assumed most of the time. And sometimes you can't have such a clear view of the problem because it already got too messy at another stage of the process, and you want to murder the system designer plus maybe some of your coworkers too.
Of course, for certain tasks long functions are still reasonable, but they tend to be the exception rather than the rule, and it takes quite some practice to realize.
Best practical advice: do more design and planning on paper.
- crpatino 6y agoA program composed of one thousand functions with average length of 1K LOC is jarring and hard to understand. The same program refactored into a quarter of a million functions of 5-8 LOC each is jarring in a very different way. At the end of day we are talking of a million LOC, and no level of granularity is going to protect your brain against that amount of information. What you need at those levels is modules. Dozens of short functions (though 5 LOC is overkill IMHO) that work together to solve a single, well constrained problem. Whatever its other shortcomings, Object Oriented Programming gives you that out of the box, though you have to design your classes instead of just banging out code as you go. And then you can create packages, - modules that use number of tightly coupled classes to solve a single, well constrained problem.
- girvo 6y agoI’m always much more proud of a good clean function I’ve written than I am a larger, less clean one. The shorter one takes longer to write, and requires more understanding and thought — and it’s usually the second or third pass refactoring the long less clean one, but it’s always satisfying when I get there. Easier to test usually, too.
- slx26 6y agoYeah, you just feel you actually got it right.