🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3382

Re: How to eliminate GOTO?

#3382
From: Michael Smith
Date: Sun, 27 Aug 1995 11:51
56 lines
2750 bytes
Much of the discussion on got vs loop tests comes down to a matter of 
taste.  My own rule of thumb is that if a "goto" must be used, the goto 
and the label should be visible on the same screen.  If it is necessary for 
a programmer to go searching for the label then you have the beginnings 
of spagetti code.  

Consider the example:

for (i=0; i<MAX_I; i++) {
    for (j=0; j<MAX_J; j++) {
        for (k=0; k<MAX_K; k++) {
            do_something( i, j, k );
            if (error_condition) goto END_LOOP
        }
    }
}
/*************/
END_LOOP:
/*************/
do_cleanup();

This code is clear and efficient and a good case could be raised for the 
use of the "goto" instead of 3 levels of loop sentinals (or 2 levels plus 
a break).

This argument breaks down, however, if each loop has many lines of code.  
A maintenance programer will then have to search for the label and the 
danger of him/her misunderstanding the logic is much higher.  If a large 
amount of code is contained in the loops then the overhead of checking 
sentinals will be relatively small (both in lost performance and 
increased complexity).

Of course you may then ask why long and complex loops are nested 3 deep.  
It may be better to break the program into smaller subroutines thus 
reducing the complexity of each code segment.

I have seen enough "spagetti" code littered with goto statements to 
understand why many programmers will avoid the statement at all cost.  If 
your code is already complex and messy a goto is almost certain to make 
it worse.  However if your code is clean and well structured, the careful 
use of a goto statement may well help to keep it that way.

Well that's my 2.9 cents worth (Hey, I work in Australian Dollars, not US 
ones).


#####################################################################
Michael Smith                                       msmith@mpx.com.au
                     
Emmenjay Consulting 
    Making computers work for you, not the other way around.

PO Box 909                                             Ph 018 240 704
Kensington 2033
AUSTRALIA    
#####################################################################
Technical Details
Message-ID: <41pm80$skp@inferno.mpx.com.au>
Path: arc1-ca.pugleaf.net!inferno.mpx.com.au!jolt!msmith
References: <3vn1ht$kj8@martha.utk.edu> <41cke4$gtd@sparcserver.lrz-muenchen.de> <41glno$3a0@caip.rutgers.edu> <41iebe$kec@news0.rain.rg.net>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!inferno.mpx.com.au!jolt!msmith
From: msmith@jolt.mpx.com.au (Michael Smith)
Newsgroups: comp.std.c,comp.lang.c
Subject: Re: How to eliminate GOTO?
Followup-To: comp.std.c,comp.lang.c
Date: 27 Aug 1995 11:51:28 GMT
Organization: Microplex Pty Ltd
Message-ID: <41pm80$skp@inferno.mpx.com.au>
References: <3vn1ht$kj8@martha.utk.edu> <41cke4$gtd@sparcserver.lrz-muenchen.de> <41glno$3a0@caip.rutgers.edu> <41iebe$kec@news0.rain.rg.net>
NNTP-Posting-Host: jolt.mpx.com.au
X-Newsreader: TIN [version 1.2 PL2]