🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.lang.c
Article #9190

C-STYLE (goto?)

#9190
From: Jeffery A. Cavallaro
Date: Fri, 22 Aug 1986 21:41
62 lines
2246 bytes
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
Technical Details
Message-ID: <3253@brl-smoke.ARPA>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!brl-adm!brl-smoke!smoke!jeff@isi-vaxa.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!brl-adm!brl-smoke!smoke!jeff@isi-vaxa.ARPA
From: jeff@isi-vaxa.ARPA (Jeffery A. Cavallaro)
Newsgroups: net.lang.c
Subject: C-STYLE (goto?)
Message-ID: <3253@brl-smoke.ARPA>
Date: Fri, 22-Aug-86 17:41:05 EDT
Article-I.D.: brl-smok.3253
Posted: Fri Aug 22 17:41:05 1986
Date-Received: Sat, 23-Aug-86 05:06:19 EDT
Sender: news@brl-smoke.ARPA
Lines: 62