Coding Standards. was: a style question

Doug Gwyn gwyn at smoke.brl.mil
Sun Nov 18 01:13:58 AEST 1990


In article <i7NRs3w163w at phoenix.com> stanley at phoenix.com (John Stanley) writes:
>   The main advantage to having one exit point is that it is easier to be
>sure any necessary cleanup is performed prior to exit.

A lot of the code in BRL's MUVES project has functions implemented more or
less along the following lines:

bool function( ... ) {
	...
	if ( problem )
		{
		ErSet( CODE_0 );
		goto err_0;
		}
	...
	if ( problem )
		{
		ErSet( CODE_N );
		goto err_n;
		}
	...
	return true;	/* success */

	/* error handling consolidated here: */

    err_n:
	fclose( fp );	/* for example */
	...
    err_0:
	FreeList( tp );	/* for example */
	...
	return false;
	}

The idea is to make sure the sequential actions are unwound in reverse
order when an error occurs.  While there are numerous other methods,
this one has worked rather well in practice.  It is also a good example
of structured use of "goto".



More information about the Comp.lang.c mailing list