Jumping into blocks is a no-no (was: Re: An amusing piece of code)

Joseph S. D. Yao jsdy at hadron.UUCP
Thu Apr 17 16:04:29 AEST 1986


In article <275 at copper.UUCP> stevesu at copper.UUCP (Steve Summit) writes:
>	if(stbuf.st_uid != getuid())
>		{
>nope:		printf("I don't think you want to delete this file.\n");
>		return;
>		}
>	if(strcmp(name, "vmunix") == 0)
>		goto nope;
>	unlink(name);
>	return;

	if (stbuf.st_uid != getuid() ||
	    strcmp(name, "vmunix") == 0) {
		printf("I don't think you want to delete this file.\n");
		return;
	}
	unlink(name);
	return;

If the conditions get any more complex, have your function return
a value, and let the calling function perform the error condition
(or not) depending on the value.  It is  n o t  generally agreed
that, just because goto's are used au hazard in UNIX code, that
this is a good thing, even for error returns.  I will defend the
use of goto's in certain cases (and again cite Knuth), but this is
not one of them, nor is any other attempt to jump into blocks.

The pathological case (which occurs quickly) is called "spaghetti
code."  It is the bane of software engineers' lives.

Oh, by the way, there are a lot of people who believe that examining
UNIX source code shows you a lot of good code and a heckuvalotta very
bad code.  Case in point.
-- 

	Joe Yao		hadron!jsdy at seismo.{CSS.GOV,ARPA,UUCP}



More information about the Comp.lang.c mailing list