C Style

Richard L. Klappal rlk at chinet.UUCP
Wed Sep 11 17:50:02 AEST 1985


The question:
	Which of the following code segments is more understandable,
	(readable, structured, etc) given the current attitudes
	about the presence of goto's in programming?

(For the sake of argument, assume I left the comments out of both
versions entirely.  I included them here in case people are not clear
what the various curses routines do.)


GOTO VERSION:
	. . .
	noecho();		/* turn off echo */
retry:
	move(4,10);		/* set cursor position */
	refresh();		/* update screen */
	ch = getch();		/* get input character (without echo) */
	if ((ch < '1' || ch > '5') && ch != 'E')
	{			/* valid input is 1 thru 5 or 'E' */
		putchar(BELL);	/* sound bell on invalid input */
		goto retry;
	}
	addch(ch);		/* echo the valid character */
	refresh();		/* update screen */
	. . .

versus NO GOTO VERSION

	for ( ; (((ch=getch()) < '1' || ch > '5') && ch != 'E') ; )
		putchar(BELL);
	addch(ch);
	refresh();



My background is predominately from a FORTRAN-BASIC-PL/I
environment, so I tend to think of the FOR (.;.;.){} in terms
of DO .. CONTINUE, iterative DO .. END or DO WHILE .. END constructs,
where this kind of an assignment in the conditional is verboten.

I guess what I'm really asking is:
	If you had to modify this program, written by someone else who
	commented it sparsely, which style would you prefer to work on?


-- 

Richard Klappal

UUCP:		..!ihnp4!chinet!uklpl!rlk  | "Money is truthful.  If a man
MCIMail:	rklappal		   | speaks of his honor, make him
Compuserve:	74106,1021		   | pay cash."
USPS:		1 S 299 Danby Street	   | 
		Villa Park IL 60181	   |	Lazarus Long 
TEL:		(312) 620-4988		   |	    (aka R. Heinlein)
-------------------------------------------------------------------------



More information about the Comp.lang.c mailing list