C bites / programming style [if

jrife at fthood jrife at fthood
Tue Sep 10 23:34:00 AEST 1985



Sorry that this response is rather long, but here goes...

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.
Now, I personally have a hard time figuring out which brace belongs to which,
in this case.  For one thing the braces around "return(FAIL)" are unnecessary,
and this creates extra confusion, this time around.

>	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);
}

This is a program that, although useless, will compile with no problem.  I
think this points out the fact that only one logical statement is allowed after
any of the control structures.  The braces are required to turn more than one
physical statement into one logical statement.  Note, too, that I leave the
original level of code flush with the right margin.  This keeps the style
correct, and is probably the explanation that you sought as to where I picked
up this practice.  I feel that the edge of the {paper,screen} does an adequate
job as a delimiter, and think the extra white space is a waste.

Secondly, my dislike for the construct that puts the braces in line with the if,
while, or whatever, is shown by the the source to uusnap.  Take a look at it,
and you'll probably agree that some of those "{ statement" lines tend to get
overlooked.

--

					*********************************
					*				*
					* Jeff Rife			*
					* ihnp4!uiucuxc!fthood!jrife	*
					*				*
					* "Sorry about that, Chief."	*
					*	--Maxwell Smart		*
					*				*
					*********************************



More information about the Comp.lang.c mailing list