Promoting Python

Chris Angelico rosuav at gmail.com
Wed Apr 6 13:51:24 EDT 2016


On Thu, Apr 7, 2016 at 3:04 AM, BartC <bc at freeuk.com> wrote:
>
>> I get a very strong impression
>> that you've never had to maintain appalingly written code.  The overuse
>> of GOTO will certainly help in that area.
>
>
> (I've not defending its use, but there are good reasons for retaining it.
>
> Suppose you had the job of translating language X to Y. X contains control
> structures that don't exist in Y. Or it maybe it just uses GOTO. Would the
> task be easier if Y had GOTO, or without?)

Before you say whether the task's easier, you have to first define the
task. Do you have code in a language for which there are no
compilers/interpreters that run on modern hardware, and your sole goal
is to get it runnable again? Are you trying to write idiomatic and
maintainable code in the target language? Or are you just trying to do
this for the sake of doing it, and aren't bothered by the fact that
your code won't be at all sensible in the target language?

Whenever you reimplement code from one language into another, you have
to first take a step back and figure out what the code is attempting
to do. Otherwise, all you're doing is reimplementing, line by line and
operation by operation, in a different syntax. And sure, having GOTO
would make that easier. But I've ported a lot of code from language to
language, and the *ONLY* time I've ever reached for GOTO was
reimplementing this idiom in C:

for foo in foo_collection:
    if is_what_we_want(foo):
        use_me = foo
        break
else:
    use_me = Foo()
    foo_collection.append(use_me)
use(use_me)

In C, 'for' loops don't have 'else' clauses, so I ended up replacing
the 'break' with a goto. Would it have been harder if C hadn't had
'goto'? Not hugely - I'd just have needed some kind of sentinel and an
'if' after the loop. And that was one tiny part of a
several-thousand-line C program, so it didn't make much difference.

ChrisA



More information about the Python-list mailing list