4 ms·
Write short methods Although this is OO dogma, it's contradicted by the research literature. Empirical studies of function length show no benefit to shorter fu
by gruseom 17y ago
Write short methods
Although this is OO dogma, it's contradicted by the research literature. Empirical studies of function length show no benefit to shorter functions. Several studies have found them to be correlated with higher defect rates and costs (up to a threshold of a few hundred lines, at which point defects and costs start to rise again).
I was suprised when I first heard this. I think it's explained by the fact that adding function calls is not logically free: it adds conceptual overhead. Factoring code into shorter functions for its own sake doesn't produce benefit that exceeds this cost. There's a fundamental tradeoff here, which a dogmatic belief in short (or long) functions just misses.
Of course, the higher-level the language the shorter all your functions become.
- spectre 17y agoMore important then writing short methods is consideration of the methods purpose and name. Any method should have only one purpose and that purpose should be expressed in its name. If it can't be expressed that way the function is to complex.
- grandalf 17y agoTrue -- And some code I've read (such as Rails) uses an options hash that gets augmented and passed all over the place. This, I think, diminishes readability and could (in the wrong hands) endanger reliability.