?

Log in

Sun, May. 31st, 2009, 09:52 pm
Maybe goto isn't so bad after all.

I've used this pattern a lot over the last few days:

for eggs in spam:
    for jello in eggs:
        if jello is the one I want:
            break
    else:
        continue
    break
else:
    return
do stuff with eggs and jello


People familiar with my code will find this amount of complex control flow unsurprising. But the reasoning here is different from my past practices. In the past I always optimized to have as few lines of code as possible, regardless of how upside down and backwards it made everything be organized, but that could be done much better in this case like so:

for eggs in spam:
    for jello in eggs:
        if jello is the one I want:
            do stuff with eggs and jello
            return


There are two reasons I find this construct objectionable. First is that it leads to a lot of code having a lot of indentation (the 'do stuff' is in many cases dozens of lines) which isn't good for readability. Second is that the natural flow of the code is 'scan for the thing I want, and then do something with it' which is the flow of the structure I've been using. I suppose one could write it like this:

def my_little_func():
    for eggs in spam:
        for jello in eggs:
            if jello is the one I want:
                return eggs, jello
    return None, None

eggs, jello = my_little_func()
if eggs is None:
    return
do stuff with eggs and jello


But that's hardly elegant and forces the flow to jump around in terms of the visual layout of the code, which goes against what I'm trying to accomplish.

At the risk of starting a religious war, I'd like to point out that the most readable and versatile way of doing what I want is like so:

for eggs in spam:
    for jello in eggs:
        if jello is the one I want:
            goto x
return
x: do stuff with eggs and jello


But short of that the following may be the best option currently available. I might switch to it:

for eggs in spam:
    for jello in eggs:
        if jello is the one I want:
            after_func(eggs, jello)
            return

def after_func(eggs, jello):
    do stuff with eggs and jello


But it still doesn't look as nice as the version with a goto.

Mon, Jun. 1st, 2009 06:34 am (UTC)
muzyka_sfer: religious propaganda

ah,. you are a victim of religious propaganda, too..

The simple fact is that the motto "goto is considered harmful" was propagated by an extremist, mr Djikstra, and in reaction to the previous programming practices, which they labelled as "spagetti code". What came after the war was the "structured programming paradigm", which technically manifested itself in building operators for the creation of subroutines and functions into programming languages.

The fighters of this Reformation, however, did not realize that "goto" itself is sufficient for an elegant minimalistic way of subroutine creation, and that this technique avoids overhead plus restrictions on arguments and return results etc.
Actually, the question of utility of "goto" as a language operator is completely independent of the question of need to structure larger programs - but historically, falsely, they became inseparable. Goto got cursed, and remained one of the strongest taboos of programming: as usual, humanity was saved from itself by forcing unto it the ways to do things, and heretics were damned so strongly that to this very day every bloody author on every book on programming feels obliged to appologize profusely when mentioning the "goto" operator, exactly once in the whole text and not to remember the damn thing afterwards, ever.
The witchhunt continued for decades, I have a university publication from 1994, "researching" (!!) the ways goto can be eliminated from programs - at the expense of creating complicated set of moves totally upturning logic and readability.

In fact, there existed a different position even during time of Religious Wars, much more moderate that that of extremist Djikstra. You might be surprised to learn that one of the moderates' names was Donald Knuth, who published an article advocating the use of the operator when needed. As far as I remember, he even cited examples of proper and even beautiful uses of "goto".

What is even less known, at least in this context, is that Knuth offered his own paradigm in competition to Structured Programming. Many people heard the name of it, but few, amazingly few (considering the simplicity of the approach) know what that term means: Literate Programming.
L.P. is not "a documentation tool" - it is a programming paradigm, that allows structuring without the use of built-in functions/subroutines for human readability only.
L.P. basically is (a) writing programs in pseudocode, whereupon arbitrary phrases in a human language become precise operators of a "meta-language" created at the moment of programming
(b) and a way to provision for programming in the order of thinking, i.e. not as machine instructions demand, but the way humans logically do it.

Having hit upon the idea and tried it, Knuth wrote of the feeling of relief from the "structured programming" practices and proclaimed that L.P. was the true way to program. Regrettably, not that many noticed.

After Knuth's first Web tools for L.P. others were created, simplifying the concept and detangling ot both from the TeX and interdependence with a particular programming language, the most accessible one is "noweb" by Ramsey.

To return to the question of "goto" and finish with it, let me tell also that even enemies of "goto" during those Wars admitted that it works faster (at least for C implementations) than built-in subroutines/functions, the latter besides restricting the programmer's thinking involve unneccessary overhead.
The use of built-in functions is justified for recursion, for instance, when implicit shuffling behind the scenes is used for a good purpose, but can be avoided when subdivision is created simply not to lose the thread when thinking.

And the idea of structuring the program into precise equivalent of subroutines/functions with goto statements (which produces a clear, readable layout as well) is very, very true, while being totally unknown to the programming masses today.

So o hell with extremist Djikstra.
As far as I am concerned, Goto is considered helpful

Mon, Jun. 1st, 2009 12:45 pm (UTC)
g253: Re: religious propaganda

Agreed. And I think Bram's example is a very good illustration. I've always liked "elegant" code, and always thought this whole goto bashing was blown out of proportion. It shouldn't be overused, and sometimes it's not the best construct, but gotos are a perfectly legitimate method of flow control.