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

Chris Calabrese[mav] cjc at ulysses.homer.nj.att.com
Sat Dec 24 03:58:47 AEST 1988


In article <271 at twwells.uucp>, bill at twwells.uucp (T. William Wells) writes:
> In article <8 at estinc.UUCP> fnf at estinc.UUCP (Fred Fish) writes:
> ...
> : 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.

This is yet another tripple icky example of CS professors
thinking that just because they can get the example programs
in K&R to compile, and because they did work in Fortran in
their thesis on astrophysics, they are qualified to teach
C (or any other language).  Wrong, wrong, wrong.

Any C guru qualified (in my humble opinion) to teach
C would be trying to teach code which looks like:

	/*
	 * assume students have not learned the ',' operator yet
	 */
	do	{
		inchar = getchar();
		} while(inchar != EOF && inchar);
	return inchar;

Personally, I haven't decided whether the while should be lined
with the do or the loop body yet, but I like the brace alignment
this way.
-- 
	Christopher J. Calabrese
	AT&T Bell Laboratories
	att!ulysses!cjc		cjc at ulysses.att.com



More information about the Comp.lang.c mailing list