while true: !!!

Steve Lamb grey at despair.rpglink.com
Mon Dec 18 06:18:59 EST 2000


On Mon, 18 Dec 2000 01:19:51 -0700, Blair Lowe <Blair.Lowe at compeng.net> wrote:
>This is exactly what a python program should be NOT doing. "while 1" 
>is one of the most cryptic statements in Computer Science. 

    Really?  I think it is actually one of the most obvious.  Esp. when one
takes into consideration that you generally learn it once while learning how
different people define their own way of doing an endless loop is a once per
program experience.

>other languages are known for this stuff, but I do not know why python 
>doesn't auto define true and false, so that we don't have to write unreadable
>stuff.

    Maybe because defining a true and then making it false is more cryptic
than an endless while with clear escapes from the loop?  

>While "while 1" (pardon the pun) is a standard fixture that anyone 
>with any experience knows what is going on, the whole point is to 
>make all our code readable so that we, and others can maintain it and 
>reuse it easily.

    I sure hope that isn't the case.  As many people have rightfully pointed
out on a great many things regarding computers, when you design something for
idiots only idiots will use it.  "while 1:" in Python is a very easy concept
to graps compared to, say, basic file IO or, heaven forbid, regular
expressions!  If someone can decode (much less write) the following line...

    self.re = re.compile(r'^(\d{1,2}|)[Dd](\d{1,3}|)([+-]\d{1,2}|)$')

     ... then even if they have /never/ seen "while 1:" before they will know
wiat it is.  And guess what, people are going to have to know regex if they
are going to maintain other people's code.  

>Imagine the newbie looking at this statement: while 1 ... hmmm must 
>be the first condition of a while loop ????

    Imagine a newbie maintaining someone else's code.  Wait, a newbie wouldn't
be maintaining someone else's code.  A newbie would be reading through
Python's web page and hit the documentation for while at
<http://www.python.org/doc/2.0/ref/while.html> or maybe even reading the
Beazley book and hit Chapter 5, Control Flow, read up on while there and even
see an example of "while 1:".  In fact, the example given is reading lines
from STDIN until a blank line is entered.  

>It is cheap to define true and false once, or (my favourite) define 
>ever (;;). Although the definition is cryptic, the resulting code is 
>very readable. That is what we want.

    Is it?  Readable for whom?  Personally, dealing with Perl code day in and
day out, I find the "line noise" quite readable.  Others dislike it but there
are things about it I do like.  I also find regex quite readable.  Let's take
the previous example.

    ^(\d{1,2}|)[Dd](\d{1,3}|)([+-]\d{1,2}|)$

    Now, how would you rewrite that to be readable?  Well, we could expand all
of the \d's to ::DIGIT:: for example, but does that make it any more readable?

    ^(::DIGIT::{1,2}|)[Dd](::DIGIT::{1,3}|)([+-]::DIGIT::{1,2}|)$

    Does that help?  Well, lets expand a few more.  ^ and $ aren't obvious, so
let's make that ::BOL:: and ::EOL::.  Not that those are obvious either, but
at least one might be able to puzzle out what they mean.  Oh, and | for "or"?
Tsk, tsk.  Not to mention [Dd] meaning D or d?  That won't do at all, either!

    ::BOL::(::DIGIT::{1,2}::OR::)D::OR::d(::DIGIT::{1,3}::OR::)(+::OR::-::DIGIT::{1,2}::OR::)::EOL::

    Yeah, that doesn't exist, but it is a play on the POSIX regex of having
character classes defined as words to be "readable" instead of, well, being
readable.  "for ever" and "while true" are cute, but aren't really needed
except for those who are more worried about the look of the language over the
function.  "while true" dictates that true, at some point, equals false.  All
to avoid a break or next (depending on the language)?  No thanks.  "for ever"
means having multiple forms after for.  That breaks consistancy.  All for
"readabilty".  Certainly not for maintainability since consistancy, to me,
means better maintainability, not less.  If you disagree, maybe you'll tell me
why the following is so nice in Perl.

if (!$foo){
  next;
}

next if (!$foo);

unless {$foo){
  next;
}

next unless ($foo);

    I can tell you why three of the four forms are bad and, although I like
two of them, only the remaining one should be used.  Maintainability.

if (!$foo){
  next;
}

    That is the best one to use because if the statement needs to be change to
true you're not changing unless to if.  It is better than placing the
operation before the test because in that form you cannot have multiple
operations.  What if you need to add a number and then do a next?  Well, now
you have to rewrite the code.  And finally unless does not allow an "else
unless".  I don't recall if else is allowed, either.  

    So, getting back to your "for ever" statement.  Yeah, it looks pretty, but
it costs more work in the end and causes more thinking for people beyond the
novice stage.  Sure, everyone is a novice once, but hardly anyone who uses any
language for any length of time is a novice forever.

    IMHO trying to cater to the novice at the expense of maintainability for
the experts is not something to strive for in a language.  Introducing a
design where a statement is contradictory is also not something to strive for.
That is why we have "while 1:" because it is obvious to all who see it what it
is beyond, say, the first day or so with the language and it doesn't have the
problems "for ever" and "while true" impose.

-- 
         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