The rap against "while True:" loops

Ethan Furman ethan at stoneleaf.us
Mon Oct 12 14:22:17 EDT 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.
> 

What's wrong with breaks?

I'll agree that they can be overused and abused, but I am not aware of 
*any* programming costruct that cannot be.  If you rule out one way of 
doing things for every situation you can end up making messes just as 
bad as the ones you're trying to avoid.

Good programming, as all good endeavors, requires thinking too.

~Ethan~



More information about the Python-list mailing list