The rap against "while True:" loops

John Reid j.reid at mail.cryst.bbk.ac.uk
Mon Oct 12 20:02:52 CEST 2009


Mensanator wrote:
> On Oct 12, 3:36�am, greg <g... at cosc.canterbury.ac.nz> wrote:
>> Mensanator wrote:
>>> while not done:
>>> � � ...
>>> � � if n==1: done = True
>>> � � ...
>> Seems to me that 'while not done:' is no better than
>> 'while True:', because in both cases you have to look
>> inside the loop to find out what the exit condition
>> is.
>>
>> Using a more meaningful name for the flag can help,
>> but you can't teach someone that just by giving them
>> an overly simplified rules such as "never use
>> while True:". They'll probably just replace it with
>> 'while not done:' and think they've improved things,
>> without ever really understanding the issue.
> 
> You're missing the point. It's not that you have to
> look inside for the terminating condition. It's that
> you don't need a break.

Nothing wrong with a having a break IMHO.

while not done:

seems very dangerous to me as you'd have to

del done

before writing the same construct again. That's the sort of thing that 
leads to errors.




More information about the Python-list mailing list