is there any principle when writing python function

Tobiah tobiah at teranews.com
Fri Aug 26 17:48:28 CEST 2011


> Furthermore: If you are moving code out of one function to ONLY be
> called by that ONE function then you are a bad programmer and should
> have your editor taken away for six months. You should ONLY create
> more func/methods if those func/methods will be called from two or
> more places in the code. The very essence of func/meths is the fact
> that they are reusable.

While I understand and agree with that basic tenet, I think
that the capitalized 'ONLY' is too strong.  I do split out
code into function for readability, even when the function
will only be called from the place from which I split it out.

I don't think that this adds to the 'spaghetti' factor.  It
can make my life much easier when I go to debug my own code
years later.

In python, I use a small function to block out an idea
as a sort of pseudo code, although it's valid python.  Then
I just define the supporting functions, and the task is done:

def validate_registrants():

         for dude in get_registrants():
                 id = get_id(dude)
                 amount_paid = get_amount_paid(dude)
                 amount_owed = get_amount_owed(dude)

                 if amount_paid != amount_owed():
                         flag(dude)

I get that this cries out for a 'dude' object, but
I'm just making a point.  When I go back to this code,
I can very quickly see what the overall flow is, and
jump to the problem area by function name.  The above
block might expand to a couple of hundred lines if I
didn't split it out like this.



More information about the Python-list mailing list