The rap against "while True:" loops

Mensanator mensanator at aol.com
Tue Oct 13 11:20:21 EDT 2009


On Oct 13, 3:44�am, John Reid <j.r... at mail.cryst.bbk.ac.uk> wrote:
> 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.

No, it's just that the OP was asking whether
avoiding "while True" is considered Best Practice.
How can you answer such a question without sounding
dogmatic?

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

"Just another line that has to be interpreted later"
is a strange comment in the context of "del done".

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

Do you consider Perl golf to be Best Practice?




More information about the Python-list mailing list