The rap against "while True:" loops

John Reid j.reid at mail.cryst.bbk.ac.uk
Tue Oct 13 04:44:22 EDT 2009



Mensanator wrote:
>> Nothing wrong with a having a break IMHO.
> 
> My opinion is that there is everything wrong with
> having a break. I don't think I have ever used one,
> I write code that doesn't depend on that crutch.
I guess its crutch-iness is in the eye of the beholder. You seem to have 
a dogmatic view about this.

> 
>> 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.
> 
> Duh. I won't write silly code like that either.
> If I need more than one loop structure then I'll
> do something like
> 
>     while not done_with_this
> 
> 
>     while not done_with_that
This is neither clean or well scoped.

> 
> Besides, since I _always_ initialize the flag
> before entering a loop, the flag can be reused
> and doesn't have to be deleted (as long as the
> loops aren't nested). And since I don't use goto,
> there's no chance the initialization can be avoided.
Initialising the flag is just another line of code that has to be 
interpreted later. I didn't notice the initialisation in your original post.

> 
> The best way to avoid the pitfalls of spaghetti
> code is to not write it in the first place.
> 
I agree. With 'break' it is obvious what the code does and there are 
fewer lines to write in the first place and comprehend in the second.




More information about the Python-list mailing list