do...until wisdom found...

Steve Lamb grey at despair.rpglink.com
Tue Apr 17 17:26:48 EDT 2001


On Tue, 17 Apr 2001 13:51:32 -0700, Dennis Baker <drbaker at softhome.net> wrote:
>long time and make for more readable, easier to debug code.  For Example the 
>construct above could be rewritten:

>set up for first cycle
>while !c:
>   whatever needs to be done
>   set up for next cycle

    I had a rather... livid discussion about this the last time this entire
fiasco came up and suggested the very same thing.  It was duly shot down
(Thanks again, to Alex) with a simple retort.

    How do you know what to set the initial value to?  In some cases you do,
in some cases you don't.  In some cases in later revisions that initial value
may be used for some statistical analysis inside the loop and providing
invalid data would throw off that analysis.  Furthermore, it removes
protection against uninitilized variables, which is something that is valid in
Python.

    Yes, in some cases setting up the while loop to contain the trigger at the
while is preferred, but this it not the case in /all/ cases.  To state
absolutely that the while 1: idiom is poor programming is to fall into the
same trap that any absolute argument falls into...  There are exceptions.

    Off the top of my head.

file = open(foo)
line = file.readline()
while (line):
    do_stuff_with_line()
    line = file.readline()

    This is what you would have someone do instead of the idiomatic way which
is as follows

file = open(foo)
while 1:
    line = file.readline()
    if not line:
        break
    do_stuff_with_line()

    The proble with the former and not the latter is what happens when we want
to make a change in what we call for file.readline()?  In the former we have
to change it in two places, the latter, one.  It is arguable that the latter
case, the case that you suggest is a poor programming practice, is actually
easier to read and maintain since we're not doing something twice which means
devining what is happening twice and changing it twice if at some later time
we need to do so.  IE, the latter for more readable and easier to debug (aka,
maintain).

    That was a hard lesson for me to learn.  However, having applied to
another language, Perl, my coding has certainly improved in both readability
and maintainability.
  
-- 
         Steve C. Lamb         | I'm your priest, I'm your shrink, I'm your
         ICQ: 5107343          | main connection to the switchboard of souls.
-------------------------------+---------------------------------------------



More information about the Python-list mailing list