Short-circuit Logic

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


On 05/30/2013 05:58 AM, Chris Angelico wrote:
> On Thu, May 30, 2013 at 10:40 PM, Roy Smith <roy at panix.com> wrote:
>> if somebody were to accidentally drop three zeros into the source code:
>>
>>> x = 1000
>>> while x < 173:
>>>      print(x)
>>>      x += 1
>>
>> should the loop just quietly not execute (which is what it will do
>> here)?  Will that make your program correct again, or will it simply
>> turn this into a difficult to find bug?  If you're really worried about
>> that, why not:
>
> 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? ;).

--
~Ethan~



More information about the Python-list mailing list