Assignment Versus Equality

Chris Angelico rosuav at gmail.com
Tue Jun 28 06:34:59 EDT 2016


On Tue, Jun 28, 2016 at 6:49 PM, Lawrence D’Oliveiro
<lawrencedo99 at gmail.com> wrote:
> On Tuesday, June 28, 2016 at 7:26:30 PM UTC+12, Chris Angelico wrote:
>> Why not:
>>
>> if (error) goto cleanup;
>> ...
>> cleanup:
>> do_cleanup;
>
> They all fall into that same trap. Doesn’t scale. Try one with allocation inside a loop, e.g. lines 488 onwards.

How is that different? You can use a goto inside a for loop just fine.
(You can even, if you are absolutely insane and begging to be murdered
by the future maintainer, use a goto outside a for loop targeting a
label inside. See for example Duff's Device, although that's a switch
rather than an actual goto.) You have a loop, and inside that loop,
the exact same error handling pattern; so it should be possible to
perform the exact same transformation, albeit with a differently-named
local cleanup label.

So, yes. It doesn't scale, if by "scale" you mean "so many separate
local instances of error handling that you lose track of your cleanup
labels". But you should be able to keep half a dozen labels in your
head (if they're named appropriately), and if you have that many local
error handlers, you probably want something to be refactored.

ChrisA



More information about the Python-list mailing list