Always use braces (was: Suggested new C loop syntax)

T. William Wells bill at twwells.uucp
Fri Dec 23 21:06:17 AEST 1988


In article <8 at estinc.UUCP> fnf at estinc.UUCP (Fred Fish) writes:
: Since this is my pet peeve #1 about C style, I couldn't resist.  There
: is no ambiguity if you simply make it a habit to ALWAYS include braces
: for {if, while, do, for} etc, even when they are not strictly necessary.

Absolutely. One should always include optional braces.  There are two
advantages:

    1) A decrease in the likelyhood of programming errors.

    2) An increase in the readability of the program.

One should also never write a compound statement on the same line as
the head of the statement it is embedded in. E.g., instead of:

	for (a; b; c) statement;

one should write:

	for (a; b; c) {
		statement;
	}

Doing that makes finding the statement quite a bit easier.

: I was recently helping out a freshman CS major with her C homework and
: found out, to my horror, that the teacher in the course was marking
: off points for code which included braces when they weren't strictly
: required.  They were teaching her to write code like:
:
:       inchar = getchar ();
:       while (inchar != EOF)
:               if (inchar == 0)
:                       goto done;
:               else
:                       inchar = getchar ();
: done: return inchar;
:
: Ickkk!!!

Double Ickkkk!!!! A goto where a break would have done.

---
Bill
{uunet|novavax}!proxftl!twwells!bill



More information about the Comp.lang.c mailing list