The rap against "while True:" loops

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Oct 12 04:26:44 CEST 2009


En Sun, 11 Oct 2009 19:46:06 -0300, Philip Semanchuk  
<philip at semanchuk.com> escribió:
> On Oct 11, 2009, at 5:51 PM, bartc wrote:
>> Mensanator wrote:
>>> On Oct 10, 3:15�pm, kj <no.em... at please.post> wrote:

>>>> I'm coaching a group of biologists on basic Python scripting. �One
>>>> of my charges mentioned that he had come across the advice never
>>>> to use loops beginning with "while True".[...]
>>>
>>> If you know this "exit that is always reached",
>>> why do you pretend not to know it by writing
>>> "while True"?
>>
>> When I'm starting to code something I haven't yet fully worked out, it  
>> often starts with an infinite loop like this, until the body is coded  
>> and I've figured out how to escape from it.
>>
>> At the end if may or may not be tidied up, depending on how much work  
>> it is to reconcile several possible break points into a single  
>> terminating condition to be place at one end, and whether that is  
>> likely to break or obfuscate a currently working program.
>
> I think you bring up a good point. I think "while True" has some  
> legitimate uses (like event loops), and I don't mind seeing it there.  
> What I don't like is goto, and to a lesser extent break, exit, and  
> multiple returns. I don't find too many cases where they're the clearest  
> way to express things. And where one sees a "while True", one can almost  
> always find a "break" or two lurking in the loop.
>
> IMHO, break, goto, etc. have their place, but they're ripe for abuse  
> which leads to spaghetti code. Since the OP is teaching non-programmers  
> to write code, I think the potential for abuse is especially important  
> to keep in mind. I'd think that teaching them a tool like "while True"  
> would encourage the "code now, design later" trap that even experienced  
> programmers -- including myself -- sometimes fall into. Writing "while  
> <condition>" instead forces one to stop at the beginning of the loop and  
> think at least a little about exactly what it's meant to accomplish.

I don't think so; forcing ALL loops to have the condition at the start is  
unnatural in some cases. Some loops are best written with the condition at  
the end (do/while or repeat/until in Pascal) and some loops require a test  
in the middle (in Python, while True: + break, because we don't have an  
unconditional loop).

> In addition, isn't it easier to figure out how this loop ends --
>
>     while (condition1) and (condition2) and (condition3):
>         ...lots of code here...
>
> than this one?
>
>     while True:
>         ...lots of code here...
>         if not condition1:
>            break
>         ...lots of code here...
>         if not condition2:
>            break
>         ...lots of code here...
>         if not condition3:
>            break

For a loop that can be written as the first one above, sure, that's pretty  
explicit.
For a loop as the second one, try to rewrite it with the condition at the  
start: you'll end with duplicated tests and/or duplicated code and/or  
artificial boolean variables added.

Some algorithms are *naturally* expressed as a loop with the condition in  
the middle. Forcing the condition to always happen at the start requires  
artificial steps that complicate unnecesarily the code.

-- 
Gabriel Genellina




More information about the Python-list mailing list