🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.misc
Article #4401

Re: GOTO controversy

#4401
From: David N. Smith
Date: Fri, 15 Mar 1996 19:38
108 lines
4326 bytes
In article <4hl8mt$4po@newshost.cyberramp.net> John Noland,
sinan@cyberramp.net writes:
>>Example for a good use of goto:
>>---                    ????
>>------------------------------------------------------------------------------
>>   HEV    hev1, hev2, hev3;     /* Event semaphores */
>>   HMTX   hmtx;                 /* Mutex semaphore  */ 
>>   void  *ptr;              
>
>>   if (!DosCreateEventSem(0, &hev1, 0, FALSE))
>>       goto hev1_failed;
>>
>>   if (!DosCreateEventSem(0, &hev2, 0, FALSE))
>>       goto hev2_failed;
>
>>   if (!DosCreateEventSem(0, &hev3, 0, FALSE))
>>       goto hev3_failed;
>>
>>   if (!DosCreateMutexSem(0, &hmtx, 0, FALSE))
>>       goto hmtx_failed;
>>
>>   if ((ptr = malloc(SOME_SIZE)) == NULL)
>>       goto malloc_failed;
>>
>>   /* Do some stuff here */
>>   return TRUE; /* We did okay */
>>
>>malloc_failed:
>>   DosCloseMutexSem(hmtx);
>>hmtx_failed:
>>   DosCloseEventSem(hev3);
>>hev3_failed:
>>   DosCloseEventSem(hev2);
>>hev2_failed:
>>   DosCloseEventSem(hev1);
>>hev1_failed:
>>   return FALSE;
>

One can introduce a flag and a subroutine. When the flag is non-zero
something has failed. The rightmost 4 bits indicate what was allocated
that needs to be released. Of course, in a real implementation, #defines
would be needed for the bits. (My C is rusty; hope I haven't written
anything too stupid.)

{
   HEV    hev1, hev2, hev3;     /* Event semaphores */
   HMTX   hmtx;                 /* Mutex semaphore  */ 
   void  *ptr; 
   int result = 0;              

   result = createStuff( &hev1, &hev2, &hev3, &htmx );
   
   if( !result )
       if ((ptr = malloc(SOME_SIZE)) == NULL)
           result = 0x100F;

   if( !result ) {
       if( result & 0x01 ) DosCloseEventSem(hev1);
       if( result & 0x02 ) DosCloseEventSem(hev2);
       if( result & 0x04 ) DosCloseEventSem(hev3);
       if( result & 0x08 ) DosCloseMutexSem(hmtx);
       return FALSE;
       }
 
   /* Do some stuff here */
   return TRUE; /* We did okay */
}


int createStuff( HEV *hev1, HEV *hev2, HEV *hev3, HMTX *htmx ) 
{
   if (!DosCreateEventSem(0, hev1, 0, FALSE))  return 0x1000;
   if (!DosCreateEventSem(0, hev2, 0, FALSE))  return 0x1001;
   if (!DosCreateEventSem(0, hev3, 0, FALSE))  return 0x1003;
   if (!DosCreateMutexSem(0, hmtx, 0, FALSE))  return 0x1007;
   return 0;
}

Now, is this better? It all depends upon how one defines 'better', I
guess. Such cascaded allocations are always a bit of a mess, and the
solution with GOTOs is clear and clean, provided it is isolated within a
subroutine so that this is ALL the code, and one can readily see the
scope of the GOTOs.

On the other hand, I've spent a lot of years writing code in a language
that doesn't have a goto at all and I sure haven't missed them, even
though I started out programming 30 years ago using FORTRAN IV and
Assembler, in which one cannot do anything without a GOTO. I wrote huge
amounts of C code a few years ago without a single GOTO and didn't miss
them then either, and think my code was better without them. 
 
However, I still think that clean code is far more important that how
well that code follows some arbitrary set of rules, even good rules.

Dave

__________________________________
David N. Smith
dnsmith@dnsmith.tjwslip.ny.k12.net (Preferred)
dnsmith@watson.ibm.com
70167.2274@compuserve.com
IBM T J Watson Research Center
Hawthorne, NY
__________________________________
Any opinions or recommendations
herein are those of the author  
and not of his employer.
Technical Details
Message-ID: <4icgv0$121g@watnews2.watson.ibm.com>
Path: arc1-ca.pugleaf.net!yasc115.watson.ibm.com!dnsmith
References: <4hhdcu$43p@due.unit.no> <4i6u62$sof@hoho.quake.net> <8080.imc@comlab.ox.ac.uk> <3147F456.50F0@simi.is>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!yasc115.watson.ibm.com!dnsmith
From: David N. Smith <dnsmith@watson.ibm.com>
Newsgroups: comp.lang.misc,comp.lang.c,comp.lang.pl1
Subject: Re: GOTO controversy
Date: 15 Mar 1996 19:38:08 GMT
Organization: IBM T J Watson Research Center
Distribution: world
Message-ID: <4icgv0$121g@watnews2.watson.ibm.com>
References: <4hhdcu$43p@due.unit.no> <4i6u62$sof@hoho.quake.net> <8080.imc@comlab.ox.ac.uk> <3147F456.50F0@simi.is>
NNTP-Posting-Host: yasc115.watson.ibm.com
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Newsreader: Nuntius 2.0.4_68K
X-XXMessage-ID: <AD6F28101502E00F@yasc115.watson.ibm.com>
X-XXDate: Fri, 15 Mar 1996 18:58:40 GMT