[net.lang.c] C format

kirk (10/22/82)

My gripe at the way C is formatted in the "white book" is not the position
of the closing 'curly bracket', but of the opening one.

Thus instead of:-

	if (......) {
		..........
		..........
	}

I prefer to use

	if (.......)
	{
		..........
		..........
	}

This makes it easier to check that brackets are paired with the one you
intended.

warren (10/22/82)

I agree with those who want to code if statements as:

if ( ... )
{
	.
	.
	.
}

Furthermore, I would make it manditory to enclose the dependent
clause in braces whenever it goes over 1 line.  There is a lot of
code out there that looks like:
if (foo)
	bar

It's way too tempting, particularly without a screen editor, just to
add another statement to the dependent clause without realizing that
it isn't part of the if anymore.

(I think that if you want to avoid the braces, put it all one one line!)


Warren Montgomery
(ihnss!warren)

trb (10/22/82)

I like
	if(foo){
		a;
		b;
		c;
	}

It's easy to see what the } delimits that way.  Just zing up the page.
The conditional IS an integral part of the block, putting the { on a
separate line belittles this fact.

As for Montgomery's suggestion for the compiler to force you to use {}
for
	if(foo){
		one statement;
	}

I think Warren should be crucified for his suggestion.  No matter
how sensible it seems, suggesting that "newline should have more
syntactic significance than it already has" at this stage in the game
is clearly heathen influenced.

The loss of Warren's talents to the Bell System will be painful to us
all, we'll miss him, but I don't think he should be able to get away
with such a suggestion.  (You KNOW about all the copycat crime these
days...)

	Andy Tannenbaum   Bell Labs  Whippany, NJ   (201) 386-6491

minow (10/23/82)

A word or two of support for the "publication" indentation style:

	if (cond) {
	    subordinate statements;
	}

The '{' and '}' have little visual significance.  Placing them in different
parts of the line -- '}' is always the left-most graphic and '{' the
right-most on the line makes them more distinct.  Thus, I would reject
the format

	if (cond)
	{
	    subordinates;
	}

Note, also, that in the publication style, the '}' is aligned with the
keyword (if/while/for, etc.) that initiated the clause.  Putting '{'
directly under the keyword obscures the visual connection.

Finally, I would highly recommend the programmer enclosing all
subordinate statements with { }.  This makes the structure explicit,
doesn't bite you when you have to add another statement (or a debugging
printout), and avoids "dangling else" problems.

Oh yes, don't ever let me catch you writing a null body on the same line:

	for (sp = string; *sp != EOS; sp++);	/* WRONG	*/

always put the ';' on the next line:

	for (sp = string; *sp != EOS; sp++)
	    ;

Martin Minow
decvax!minow

mark (10/23/82)

Popular programming languages of late (Algol 68, Ada, FORTRAN 77) that
solved dangling else with a "fi" type terminator all seem to recommend
indenting along the lines of
	if test then
		do something
	else
		don't do something
	endif
Notice that the "if" itself (and its associated syntax) is at one level
of indenting, and the nested statements are at the next level.  If you
extrapolate this indenting style to C, you get
	if (test) {
		do something
	} else {
		don't do something
	}
(You could even carry it to the Bourne extreme visible in sh and adb.)

The alternative to the above would read like
	if (test)
	{
		do something
	}
	else
	{
		don't do something
	}
which you'll notice takes an extra 3 lines.  Given the current situation that
we mostly have screens with only 24 lines, any indenting practice that makes
a big cut in how much code fits on your screen at once is probably not all
that wonderful an idea.

No, I don't advocate putting 5.5 statements per line to make an entire C
source file fit on one screen.  What I advocate is bigger screens.  But
I do prefer the more compact style.

davy (10/23/82)

#R:bunker:-17000:pur-ee:15500001:000:939
pur-ee!davy    Oct 22 22:34:00 1982



	Personally, I like the

		if (foo) {
			......
			......
		}

	constructs, because the "close-brace" lines up with the clause
that it delimits.  I have no objections to reading code which looks like:

		if (foo)
		{
			......
			......
		}

However, one guy I know insists on starting his statements on the same line
as the open brace, i.e.:

		if (foo) 
		{	......
			......
		}

If you've ever tried to edit this sort of code and place a new first statement
in the loop or whatever, you'll note what a pain it is.

I find having the open and or close braces indented to the statement level
very obnoxious and difficult to read, but then I guess this is just personal
taste.

As an aside, most of the C Coding Style documents that were posted a few 
months back recommended either

	if (foo) {				if (foo)
		.....				{
		.....		- or -			.....
		.....					.....
	}					}

Sorry for rambling on so long.....

--Dave Curry
pur-ee!davy

sjb (10/23/82)

Oh come on, why are people suggesting forcing others to abide
by their style of coding?  If you read the second sentence in
Kernighan/Ritchie, it says:

	But its absence of restrictions and its generality
	make it more convenient and effective ...

Fine, people working on a project together should decide just
what style to use, so that everyone in the group can read the
code, and people writing code that they think or know others
may have to read in the future should keep in mind that it
should be neat and easy to read, but why restrict other people?
To make a change now would lead to thousands of heartaches when
people go to recompile their old code and break their old
habits when writing new code.

pag@sri-unix (10/25/82)

How about
	if(condition)
	{
		a;
		b;
		c;
	}
??  I prefer it to the usual "if(condition){" for readability.

--peter gross