C Bites

COTTRELL, JAMES cottrell at NBS-VMS.ARPA
Tue Oct 8 09:19:29 AEST 1985


/*
> The reason I don't like:
> 	
> 	if(condition) {
> 		s1;
> 		s2;
> 	}
> can be shown by the following source:
> 
> 	if (onelock(pid, tempfile, file) == -1) {
> 		/* lock file exists */
> 		/* get status to check age of the lock file */
> 		ret = stat(file, &stbuf);
> 		if (ret != -1) {
> 			time(&ptime);
> 			if ((ptime - stbuf.st_ctime) < atime) {
> 				/* file not old enough to delete */
> 				return(FAIL);
> 			}
> ------------------------------------------------------------------------------
> 		}
> 		ret = unlink(file);
> 		ret = onelock(pid, tempfile, file);
> 		if (ret != 0)
> 			return(FAIL);
> 	}
> 	stlock(file);
> 
> When I listed the file out, the page break was right where the dashed line is.

Sorry, no dice. First off, the page break comes there regardless of how 
the `if' lines are formatted. Second off, what is the page break doing
there anyway? Don't you know about Form Feeds? Funxions on one page please.

> >	Why do you like this style?  This seems to indicate that
> >the braces are associated in your mind with the enclosed statements.
> 
> You seem to have answered your own question.  What else are the braces related
> to if not the enclosed statements?  Note the following, which has nothing to do
> with if's, while's, for's, or do's.
> 
> main()
> {
> int i = 42;
> float foo = 3.14159;
> 
> printf("Starting program\n");
> printf("i = %d, foo = %f\n",i,foo);
> 
> 	{		/* Truly local variables */
> 	static char *foo = "What is the meaning of life?";
> 	double i = 1.414;
> 
> 	printf("Another message\n");
> 	printf("i = %f, foo = %s\n",i,foo);
> 	}
> /* Back to the old variables */
> 
> printf("Yet another message\n");
> printf("i = %d, foo = %f\n",i,foo);
> exit(0);
> }

How many people axually do this? I mean use nested blox? Most people use
braces only where necessary. I have seen the trick

	#define	save(x)		{ int save; save = x;
	#define restore(x)	x = save; }

which make use of this technique, but I feel nested variables (which
usurp the scope of more global variables) are a relatively bad practice.
If it's big enuf for it's own scope, it probably should be a funxion.
 
	jim		cottrell at nbs
*/
------



More information about the Comp.lang.c mailing list