4 ms·
On the opposite end, reading long functions can be hard when they mix levels of abstraction, or their lack of abstraction buries the overall logic with details.
by danidiaz 6y ago
On the opposite end, reading long functions can be hard when they mix levels of abstraction, or their lack of abstraction buries the overall logic with details.
If I'm reading a novel, it would be jarring to read "and then he took 45 steps of average length to reach the kitchen" instead of "he went to the kitchen".
- fxtentacle 6y agoThat's where editor folding comes in.
- slx26 6y agoI 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.
- Cthulhu_ 6y agoI'm dealing with functions thousands of lines long with switch/case and if/else statements five or six levels deep, and it really is the wrong abstraction. It's massive 'handle request' functions, with a switch over a 'ok what kind of request, fetch or update or restore from backup?', then a switch over 'ok what data do you want to fetch or update or restore from backup'. That kinda thing. That's a bad example. A better example - and yes, I'm totally biased - is the main() method for the rebuild I'm working on - it's basically 'read config', 'set up logger', 'set up service x', 'set up service y', 'validate runtime dependencies like files and scripts', 'create & start http server', 'wait for shutdown signal', 'gracefully shut down http server on shutdown signal', etc. All in one place, no need to extract methods in my opinion. That said, it really is not the critical path of the application.
- ensiferum 6y agoSounds like something you can refactor into a N dimensional table with "function pointers" for handling each case.
- mangamadaiyan 6y agoThat could blow your instruction cache sky high. Depending on what you're doing, that could be something you want to avoid.
- imtringued 6y agoYou don't need function pointers. Just call the functions directly. The problem isn't the switch/if, it's the complete lack of structure.