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

Luke Paireepinart rabidpoobear at gmail.com
Tue Dec 8 03:03:29 CET 2009


If your code is not sensitive information, it might help us if you post it
to pastebin or something so we can take a look.
In general though, functions should be as long as they need to be (and no
longer!).  57 lines is not inordinately long.  If it's hard for you to read,
though, you should refactor it.

I'd say my personal hard-limit for functions before I start refactoring is
probably around 150-200 lines.  But it's rare that functions get that long
anyway.
Remember to think of them as reusable units of code that do one specific
procedure.  Once you move into OO your functions will probably end up being
rather small as well, that paradigm encourages many small functions
interacting.

On Mon, Dec 7, 2009 at 7:37 PM, Che M <pine508 at hotmail.com> wrote:

>  I have some functions that seem kind of long to me.  One of them, with
> white space, comments, print statements, and some commented-out lines,
> is 118 lines long.  If I remove all that, it is 57 lines long.  I get the
> sense
> that is inappropriately long for a Python function.
>
> The length of it is due to a number of if statements--things it needs to
> check in terms of the state of the app at the time it is called.  So there
> are a number of conditional (and "subconditional") parts to it, and what
> it does in response to those conditions.  In fact the word "if" appears in
> it 12 times.
>
> 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?
>
> Thanks,
> Che
>
> ------------------------------
> Windows Live Hotmail gives you a free,exclusive gift. Click here to
> download.<http://www.microsoft.com/windows/windowslive/hotmail_bl1/hotmail_bl1.aspx?ocid=PID23879::T:WLMTAGL:ON:WL:en-ww:WM_IMHM_7:092009>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091207/d04be0a9/attachment.htm>


More information about the Tutor mailing list