C-STYLE (goto?)

Jeffery A. Cavallaro jeff at isi-vaxa.ARPA
Sat Aug 23 07:41:05 AEST 1986


I am sure that this case of style has been brought up before.  It has been
bothering me for awhile.

Support a particular routine is supposed to perform N steps, where each step
is performed dependent on the success of the previous step, i.e., if any
step fails, then you want to clean up and return.

I realize that:

	if (
	    ( (status = step_1()) == SUCCESS) &&
	    ( (status = step_2()) == SUCCESS) &&
			.
			.
			.
	    ( (status = step_3()) == SUCCESS)
	);
	cleanup();
	return (status);

accomplishes this, but significantly ups the routine count.  Some refer to this
as good programming practice, but I tend to think of it as overkill for simple
jobs - especially when each routine requires a long header block as may be
required by various programming standards imposed upon (but maybe not accepted
by) the programmer.

Of course, there is the massively nested "if" string, but I can't stand that
style.  I tend to really get lost in such bird nests.

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).

Thanx for any and all responses (even rude ones),
Jeff



More information about the Comp.lang.c mailing list