From: cbarber@bbn.com (Chris Barber)
Newsgroups: comp.lang.c++
Subject: Re: GOTO, was: Tiny proposal for na
Date: 1 Sep 92 21:43:17 GMT
Organization: BBN Systems and Technology, Inc.

In article <rmartin.715001372@thor> 
>rgp@mpd.tandem.com (Ramon Pantin) writes:
>|From my informal counts in this thread it seems that most people are
>|perfectly happy with break, continue and mid-function returns.  Some
>|group of people avoid gotos as a rule of thumb but wouldn't mind using
>|them if required, some of these have not used a goto in the last 10 years.
>|A smaller group (only you?) avoid all of these contructs completely.

rmartin@thor.Rational.COM (Bob Martin) writes:
>I can assure you, it is not only me.  There has been a fair amount of
>private mail on the subject.  However your observation is correct.  It
>does seem that many programmers are quite happy using break and
>continue.  Moreover, it seems that a smaller, but significant number
>of programmers are content to use goto rather than structure their code.

Well I must admit that it seems that most people posting on this topic
say that they do not use gotos, even those who disagree with you. So I
guess I will just have to come out of the closet and admit that I have
been using them for several years and with no regrets. I can't recall 
finding any bugs that were related to my use of gotos and I haven't heard
any complaints about the readability of my code. In fact, I resent your
implication that because I use gotos I am lazy and write unstructured
code. Although I use gotos frequently, I use them almost entirely for 
clean up code used in error handling. For example:

My way:

void foo(void)
{
    FILE *fp = NULL ;
    char *data = NULL ;

    if ((fp = fopen("foo", "r")) == NULL)
        goto cleanup ;

    data = malloc(DATASIZE) ;

    if (DoStuff(fp, data) == ERROR)
        goto cleanup ;

    DoMoreStuff(data) ;

 cleanup:
    if (fp)
        (void)fclose(fp) ;
    if (data)
        free(data) ;
}

"Structured" version:

void foo(void)
{
    FILE *fp ;
    char *data ;

    if (fp = fopen("foo", "r"))
    {
        data = malloc(DATASIZE) ;
        if (DoStuff(fp, data) != ERROR)
        {
            DoMoreStuff(data) ;            
        }
        free(data) ;
        (void)fclose(fp) ;
    }
}

The two examples are roughly equivalent in size (ignoring whitespace).
My way is perfectly safe and maintainable as long as you ensure that
all data that is refered to in the cleanup section is initialized before
the first goto is encountered. The main difference between these is that
the second case has two levels of nesting while mine has none, and that
mine has to take a little extra time to check variable values when cleaning
up. I must admit, that for a simple example like this, I would actually
prefer the second case, but imagine what would happen if we added an 
extra 5 or 6 steps to the function, each of which could cause the 
function to abort. This would add an extra 5 or 6 levels of nesting
in the second case would making the code much harder to read. The basic
idea with my approach is to make the code readable in terms of what the
code will do when functions do not fail, in other words, I want the code
to look as much as possible like the case that does no error handling.

>Perhaps this is a contributing factor to the software crisis in this
>country, and to why our bug rate per line of code is 100 to 1000 times
>greater than it is in Japan.  I have not seen a study of such a
>relationship, and I don't know how closely the Japanese adhere to the
>principles of Structured Programming, but it is thought provoking, at
>least to me.

This would be a strong argument if you would provide documentation
about where you got the bug rate figure and could show that the 
Japanese don't use gotos. Otherwise, I think this is irrelevant.
Japense bug rates probably have much more to do with project management
techniques and philosphy than any particular coding practice.

BTW, I *did* try writing code with no gotos, breaks, and continues
several years ago but I didn't see that I was getting any particular
benefit from it so I stopped.


-- 
Christopher Barber
(cbarber@bbn.com)
Newsgroups: comp.lang.c++