[Tutor] functions--how long is too long?

Alan Gauld alan.gauld at btinternet.com
Tue Dec 8 10:27:44 CET 2009

"Che M" <pine508 at hotmail.com> wrote

> I have some functions that seem kind of long to me.  One of them, with

> I realize I can and should refactor parts that are used in other places
> in the code, but I don't there are that many in some of these.  Is
> there a better way to think about organizing this?

The length as such is not the critical factor, its the maintainability 
and readability. Can you function be easily understood by another 
programmer? Can you make changes to the function without 
changing the interface (which implies changes elsewhere in your 

Where you have a lot of if statements you might benefit from 
using a dictionary of functions or a function which returns 
a function. But there may be good reasons to keep the if 
statements too.

Sorry, but a generalised question can only get a generalised answer.
57 lines of active code is big but not ridiculous. Over 100 liones 
I would be looking closely at breaking it up just for readability.

Remember the old adage that a function should ideally fit on a 
single editor screen (or sheet of printout) - when I started that meant
25-60 lines was the range, now you can go up to 60-100 lines if 
needs be...

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list