[Tutor] How (not!) lengthy should functions be?

Danny Yoo dyoo at hashcollision.org
Thu Apr 16 19:45:07 CEST 2015


> My current understanding of function length best practice is that: 1)
> Each function should have preferably ONE clearly defined purpose.

Purpose is the biggest factor for me.


> 2) I  have seen varying recommendations as to number of lines of code per
> function,

I don't weight this as heavily.  I guess I treat physical metrics
mostly like guidelines... except for line length, for arbitrary
reasons.  :P.  Anything that goes longer than 80 characters just makes
me uncomfortable.

I do like short functions, but if some block of code is a few lines
longer than average, that's probably ok.  What I'd be looking at more
closely is the complexity of the statements *in* the function.  "Is
the function deeply nested?", for example.

Just to add: I find that unit testing has an influence on me that
encourages writing shorter functions, mostly because the unit testing
forces me to make sure the function's easy to test.  Kent Beck's "Test
Driven Development by Example" talks about this in more depth.


More information about the Tutor mailing list