Working around a lack of 'goto' in python

Stephen Horne steve at ninereeds.fsnet.co.uk
Tue Mar 9 16:43:34 EST 2004


On Tue, 09 Mar 2004 18:53:50 GMT, Joe Mason <joe at notcharles.ca> wrote:

>In article <roy-F325DF.12255009032004 at reader2.panix.com>, Roy Smith wrote:
>> OK, I'll bite.  What's wrong with exceptions for breaking out of deeply 
>> nested loops?
>
>Philosophically, the termination condition of a loop isn't an
>exceptional circumstance.

Erm - I disagree.

Philosphically, the termination condition of a loop isn't an *error*.

Most loops repeat at least several times, often many times. They only
terminate once. Therefore, termination of the loop is exceptional.

>Practically, exceptions are overkill.  Using named break (or faking it
>with goto) is "static multi-level exit", while an exception is dynamic.
>It takes more overhead at runtime, and if you don't catch it right it
>can escape up the call stack. 

As a rule, having a program that runs a few milliseconds slower is
preferable to having a maintenance headache. The reason why gotos,
breaks and continues are considered bad style in any language is
because when used inappropriately they routinely do create maintenance
headaches, and because the temptation is to use them inappropriately.

In comparison with keeping track of state variables, exceptions
probably have lower overhead, and in particular what overhead they
have is tuned to occur when the exception is thrown - it is a one-off,
rather than being repeated every time the state variable gets checked.

As for uncaught exceptions, they are easy to detect, easy to track
down, and easy to debug.

>A more pragmatic argument is that, for C++,

<snip goto example>

>Is just easier to read, and takes less typing, than

<snip exception example>

Absolutely true, and one reason why there is almost always some
latitude for using break.

There is of course no programming feature ever imagined that would
never be useful and beneficial in any circumstance. That is not,
however, a justification for including every feature ever imagined.
There will always be some cases where some unavailable feature would
have made particular jobs a little, or even a lot, easier but that's
life.

The crucial question is whether the benefits from a feature outweigh
the costs. There will always be some cases where that is a difficult
and subjective question, and different people will always assess
different pros and cons as having different weights.

Are some of us being unreasonable about gotos and/or multilevel
breaks/continues? And if so, which side are those people on?

Quite possibly 'yes' and 'both'.

As I mentioned elsewhere, I have used gotos recently but only once in
ten years or so. I have no doubt that there are many simpler cases
where at some time I could have avoided a state variable or an
exception by using a goto, but following a standard convention has
some value in itself and these days the standard convention is to use
a state variable or an exception.

These are relatively unusual cases, though. Certainly they pop up from
time to time, but in the mean time you've probably written tens of
loops that didn't need these tricks.

Personally, my vote remains 'no' but with some reservations. But then
I'm just me.


-- 
Steve Horne

steve at ninereeds dot fsnet dot co dot uk



More information about the Python-list mailing list