C style

kenny at uiucdcsb.CS.UIUC.EDU kenny at uiucdcsb.CS.UIUC.EDU
Thu Sep 26 00:41:00 AEST 1985


/* Written  6:53 am  Sep 23, 1985 by peter at graffiti.UUCP in uiucdcsb:net.lang.c */
>> 
>> 	for ( ; (((ch=getch()) < '1' || ch > '5') && ch != 'E') ; )
>> 		putchar(BELL);
>> 	addch(ch);
>> 	refresh();
>
>Obviously. A 'C' programmer would have written:
>
>	while( ((ch=getch()) < '1' || ch > '5') && ch != 'E')
>		beep();
>	addch(ch);
>	refresh();
>
>It's considered bad form to have a for loop with more than 1 empty feild.
>Now that you're using a construct that doesn't exist in FORTRAN or BASIC
>do you feel better?
/* End of text from uiucdcsb:net.lang.c */

For Heaven's sake, the big problem with this code is that the conditional
is quite unreadable in in-line code like this.  I'd prefer

	while (illegal (ch = getch()))
		beep ();
	addch (ch);
	refresh ();
	   ...
int illegal (ch)
 char ch;
 {
	return ((ch < 1 || ch > 5)
	     && ch != 'E');
 }

which at least separates the test from the action.  I realize that it also
moves the test further away physically in the code, but I think the price
is worthwhile.  

k**2          kenny at uiuc.ARPA       ...!pur-ee!uiucdcs!kenny

Disclaimer: Opinions expressed herein may not be even MINE, much less my
employer's.



More information about the Comp.lang.c mailing list