The rap against "while True:" loops

Peter Billam peter at www.pjb.com.au
Sun Oct 11 13:03:18 CEST 2009


On 2009-10-11, Bearophile <bearophileHUGS at lycos.com> wrote:
> Peter Billam:
>> I remember in the structured-programming revolution the
>>    loop { ... if whatever {break;} ... }
>> idiom was The Recommended looping structure, because the code is
>> more maintainable.
>
> I think "break" was almost the antithesis of structured programming,
> it was seen as the little (and a bit more well behaved) brother
> of "goto". Too many "breaks" turn code almost into Spaghetti,
> that is the opposite of structured programming.

It's multi-level breaks that are danger-prone and were
deprecated; a single-level break is quite safe.

> Give me a do-while and a good amount of breaks&while True
> in my Python code will be removed.

Maybe, but you still commit yourself to a rewrite if you
ever need to insert a statement after the last break-test.

It needs only one tiny extra little requirement in the
wrong place to invalidate a while or a do structure,
but the loop-and-break structure always works,
and that's what gives it its extra maintainability.

Peter

-- 
Peter Billam       www.pjb.com.au    www.pjb.com.au/comp/contact.html



More information about the Python-list mailing list