C-STYLE (goto?)

Tom Armistead toma at killer.UUCP
Thu Aug 28 03:16:47 AEST 1986


In article <3253 at brl-smoke.ARPA>, jeff at 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



More information about the Comp.lang.c mailing list