The rap against "while True:" loops
ethan at stoneleaf.us
Mon Oct 12 20:22:17 CEST 2009
> On Oct 12, 3:36�am, greg <g... at cosc.canterbury.ac.nz> 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
>>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.
More information about the Python-list