Short-circuit Logic

Ethan Furman ethan at stoneleaf.us
Thu May 30 12:30:11 EDT 2013


On 05/30/2013 08:56 AM, Chris Angelico wrote:
> On Fri, May 31, 2013 at 1:02 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
>> On 05/30/2013 05:58 AM, Chris Angelico wrote:
>>> If you iterate from 1000 to 173, you get nowhere. This is the expected
>>> behaviour; this is what a C-style for loop would be written as, it's
>>> what range() does, it's the normal thing. Going from a particular
>>> starting point to a particular ending point that's earlier than the
>>> start results in no iterations. The alternative would be an infinite
>>> number of iterations, which is far far worse.
>>
>> If the bug is the extra three zeros (maybe it should have been two), then
>> silently skipping the loop is the "far, far worse" scenario.  With the
>> infinite loop you at least know something went wrong, and you know it pretty
>> darn quick (since you are testing, right? ;).
>
> You're assuming you can casually hit Ctrl-C to stop an infinite loop,
> meaning that it's trivial. It's not. Not everything lets you do that;
> or possibly halting the process will halt far more than you intended.
> What if you're editing live code in something that's had uninterrupted
> uptime for over a year? Doing nothing is much safer than getting stuck
> in an infinite loop. And yes, I have done exactly that, though not in
> Python. Don't forget, your start/stop figures mightn't be constants,
> so you might not see it in testing. I can't imagine ANY scenario where
> you'd actually *want* the infinite loop behaviour, while there are
> plenty where you want it to skip the loop, and would otherwise have to
> guard it with an if.

We're not talking about skipping the loop on purpose, but on accident. 
Sure, taking a system down is no fun -- on the other hand, how much data 
corruption can occur before somebody realises there's a problem, and 
then how long to track it down to a silently, accidently, skipped loop?

--
~Ethan~



More information about the Python-list mailing list