3 ms·
Interestingly, research results on this come out in favor of longer functions. I read this a long time ago (in the book Code Complete) and remembered it because
by gruseom 19y ago
Interestingly, research results on this come out in favor of longer functions. I read this a long time ago (in the book Code Complete) and remembered it because it went against my assumptions. Can't get at it in Google Books but a summary appears here: http://www.augustana.ab.ca/~mohrj/courses/2003.fall/csc220/lecture_notes/routines.html http://www.augustana.ab.ca/~mohrj/courses/2003.fall/csc220/l...
Examples:
Routine size is inversely correlated with errors, up to 200 lines of code
Larger routines (65 lines of code or more) are cheaper to develop per line of code
There are all sorts of objections one can make to this. Perhaps it indicates as much about the weakness of software research as anything. But I still find it interesting enough to temper my opinions.
- pdubroy 19y agoI don't find that surprising at all. For a given piece of functionality, if it is spread across more methods, it means: - more methods to look at, and more things to hold in your short term memory - more (possible) entry points to any given piece of code, so more things to test, and greater possibility of error - more lines of code to accomplish the same task - harder to grok the overall behaviour/capabilities of a class And most of my experience with big OO libraries is with the supposed best cases: Smalltalk class libraries (Squeak and IBM) and the Eclipse framework.
- gruseom 19y agoThose are great points. Here's one issue that I think is lurking in all this. Even agreeing that a large function is easier to read than, say, 10 small ones, it doesn't follow that a system written that way is easier to understand. You still have the problem of how to factor the system into large functions as opposed to small ones - a question that is not addressed by this discussion (or, I'll bet, by the research). And it's a big problem, because complex systems usually have a great deal of intertwining, partly-but-not-completely overlapping functionality. If you could factor all this into a few large functions, you'd probably get something more readable, but in most cases you can't. So in order to build a well-factored system (with minimal duplication, separated concerns and so on) you carve things into smaller pieces that can then be cobbled together to create the various behaviors you need. (At least, that's how you do it in OO.) So the business of function length plays out quite differently at the system level than it does at the function level. The function level is easier to discuss and to study, but what matters in practice is the comprehensibility of the system as a whole. That's why your caveat, "for a given piece of functionality" is a very well-placed one. You're presupposing that the functionality has already been carved into pieces.