Coding Standards. was: a style question

Warner Losh imp at marvin.Solbourne.COM
Sat Nov 17 06:39:56 AEST 1990


In article <1990Nov10.191840.21113 at clear.com> rmartin at clear.com (Bob Martin) writes:
: The lack of standard coding practice IS a big problem for software
: maintenance.

Agreed.  However, a bad coding standard can make life miserable for
everybody.

: It specifies that functions should have single entrance and single
: exit points

This is a bogus restriction, at least in terms of a single exit point.
When you have small functions, the return statement can be used to
give meaningful flow control to the functions w/o the need for a
"goto".  I have found that when I adhere to this rule, I either get
code that looks like:

	if (allocate-memory) {
		do some stuff
		if (get-more-memory) {
			do more stuff
			if (open-file) {
				do even more stuff
				if (alloc more memory) {
					...
					status = OK;
				}
				else
					status = NO_MEM
			}
			else
				status = NO_FILE
		}
		else
			status = NO_MEM
	}
	else
		status = NO_MEM

	return status;

Or I get code that looks like:

	if (!allocate-memory) {
		status = NO_MEM;
		goto done;
	}
	do some stuff
	if (!get-more-memory) {
		status = NO_MEM;
		goto done;
	}
	do more stuff
	if (!openfile)	{
		status = NO_FILE;
		goto done;
	}

	do even more stuff

	if (!alloc memory) {
		status = NO_MEM;
		goto done;
	}

	last stuff
	
	status = OK;
done:
	return status;

When what I really want is:

	if (!allocate-memory)
		return NO_MEM

	do some stuff

	if (!get-more-memory)
		return NO_MEM

	do more stuff

	if (!openfile)
		return NO_FILE

	do even more stuff

	if (!alloc memory)
		return NO_MEM

	last stuff
	
	return OK;

A quick check reveals that the above code segments are all the same.
However, I may have missed something.  The final one is the clearest
one, IHO, of them all.  Comments....

--
Warner Losh		imp at Solbourne.COM
How does someone declare moral bankruptcy?



More information about the Comp.lang.c mailing list