🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.lang.c
Article #9214

Re: C-STYLE (goto?)

#9214
From: Tom Armistead
Date: Wed, 27 Aug 1986 17:16
88 lines
2791 bytes
In article <3253@brl-smoke.ARPA>, jeff@isi-vaxa.ARPA writes:
> I am sure that this case of style has been brought up before.  It has been
> bothering me for awhile.
> 
> The way I like to do it is:
> 
> 	setup_step_1;
> 	if ( (status = step_1()) == FAILURE )
> 	    goto abort_operation;
> 
> 	setup_step_2;
> 	if ( (status = step_2()) == FAILURE )
> 	    goto abort_operation;
> 
> 		.
> 		.
> 		.
> 
> 	setup_step_n;
> 	if ( (status = step_n()) == FAILURE )
> 	    goto abort_operation;
> 
> abort_operation:
> 
> 	cleanup();
> 	return (status);
> 
> Now, I know a lot of people detest this because of the use of goto's, but this
> seems the nicest way to perform this function.  I really have nothing against
> goto's if they are directed to the same spot (i.e., don't jump back and forth)
> and enhance the readability of the code.
> 
> Any comments?  I am willing to adapt to another reasonable style for such
> cases (if anyone really cared).

There is always the use of setjmp and longjmp to acomplish this task,
sometging like this:

#include <setjmp.h>
#define FAILURE	1	/* cannot == 0, due to setjmp conventions */

routine()
{
	jmp_buf		env;	/* envireoment for setjmp,lonjmp */
	int		val,	/* used in return to setjmp */
			status;

	if (setjmp(env) == FAILURE)	/* if abort return, cleanup and exit */
		{
		cleanup();
		return(status);
		}
	/* setup_step_1 */
	if ((status = step_1()) == FAILURE)
		longjmp(env,FAILURE);	/* return to setjmp call w/FAILURE */

	/* setup_step_2 */
	if ((status = step_2()) == FAILURE)
		longjmp(env,FAILURE);	/* return to setjmp call w/FAILURE */

		.
		.
		.

	/* setup_step_n */
	if ((status = step_n()) == FAILURE)
		longjmp(env,FAILURE);	/* return to setjmp call w/failure */

		.
		.
		.

	/* continue on from here ... */
}

This will accomplish the same thing and possibly not affend the goto haters,
although it is somewhat harder to follow if you are no farmiliar with 
setjmp() and lonjmp().

Tom
---
UUCP:
                          ihnp4\
                                \killer!toma
                                /
  drillsys!infoswx!convex!dj3b1/

Tom Armistead
Technical Details
Message-ID: <264@killer.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!columbia!caip!clyde!cbatt!cbosgd!ihnp4!cuae2!ltuxa!we53!sw013b!dj3b1!killer!toma
References: <3253@brl-smoke.ARPA>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!columbia!caip!clyde!cbatt!cbosgd!ihnp4!cuae2!ltuxa!we53!sw013b!dj3b1!killer!toma
From: toma@killer.UUCP (Tom Armistead)
Newsgroups: net.lang.c
Subject: Re: C-STYLE (goto?)
Message-ID: <264@killer.UUCP>
Date: Wed, 27-Aug-86 13:16:47 EDT
Article-I.D.: killer.264
Posted: Wed Aug 27 13:16:47 1986
Date-Received: Sat, 30-Aug-86 03:18:08 EDT
References: <3253@brl-smoke.ARPA>
Organization: The Unix(tm) Connection BBS, Dallas, Tx
Lines: 88
Summary: setjmp() and longjmp() alternatives to goto