for / while else doesn't make sense

Steven D'Aprano steve+comp.lang.python at
Thu Jun 9 04:19:23 EDT 2016

On Thursday 09 June 2016 10:34, Lawrence D’Oliveiro wrote:

> In my undergraduate Comp Sci classes, we used to discuss arbitrary rules like
> limiting functions to n lines. With real-world experience, it soon became
> clear that such rules were a waste of time. A function should be just as big
> as it needs to be, no more, no less. The same with a class, or a module. Or
> whatever other constructs your language may have.

The opposite of "arbitrary limits" is not "no limits".

An arbitrary limit like "500 lines is the maximum size a function may be" is 
clearly arbitrary and not very helpful. (Also too high.)

Better is to understand that there is no hard cut-off between "acceptable" and 
"too long", but we can still judge that all else being equal, long functions 
are worse than short functions.

The real problem is complexity of functions. The more complex they are, the 
harder they are to write correctly, and the harder to maintain, and the more 
likely that they have bugs.

The length of a function is a very crude measure of complexity, but it *is* a 
measure of complexity, and people are right to look at long functions as a code 
smell and a sign that the function probably does too much, or is badly written, 
or that it could do with some refactoring to simplify it. Not in *every* case, 
but I've never yet seen a long (> 200 lines) function that wasn't improved by 
refactoring or splitting.


More information about the Python-list mailing list