cvs commit: src/sys/dev/ed if_ed.c

Bruce Evans bde at zeta.org.au
Fri Feb 11 13:49:14 GMT 2005


On Fri, 11 Feb 2005, Poul-Henning Kamp wrote:

> In message <20050211220212.A32327 at delplex.bde.org>, Bruce Evans writes:
>
> >> : 	while (complicated-expr)
> >> : 		;
>
> Look, programming is the art of expressing intent as best as
> possible.  The single semicolon variant is very clear from
> a compiler point of view, but it is not clear from a human
> point of view.

No, both look like random punctuation from a human point of view,
but from a C programmer point of view the single semicolon is a
normal idiom while "continue;", while simple, takes a little
longer to understand since it is not normal for leaving the
end of a loop.  "continue; is for leaving the middle of a loop.

> I always use the continue variant because I feel it expresses
> intent better for two reasons:
>
> The visual impact of a word is much bigger than that of a single
> lightweight character.
>
> Writing "continue" dispells any doubt if I did it intentionally.

I disagree, and always use a single semicolon on a line by itself
to make it clear that I did it intentionally.

K&R2 seems to always use a single semicolon.  The first instance of
a loop with a null statement in K&R2 seems to be on page 48 for an
implementation of strcat():

%%%
    ...
    while ((s[i++] = t[j++]) != '\0')	/* copy */
	;
%%%

All this is idiomatic for C progammers.  If you aren't a C programmer,
then the null statement is the easiest thing in it to understand.
FreeBSD's implementation of strcat() is essentially the same, but uses
the canonical idiom for copying strings (pointers instead of arrays)
and has style bugs on every line:

%%%
	for (; *s; ++s);
	while ((*s++ = *append++) != 0);
	return(save);
%%%

(The style bugs in this are 2 semicolons not on a separate line, 0 instead
of '\0', and no space before the silly parentheses around the return value.)

Some other examples in K&R2:
    p60 "while (...)\n\t; /* ... */"
    p61 "for (...) /* ... */\n\t;"
    p71 "for (...) /* ... */\n\t;"
    p78 "while (...)\n\t;"
I stopped looking at p80 without finding any examples with "continue;" or
"/* nothing */ ;".

Guess which style is used in one-true-awk.

Bruce


More information about the cvs-src mailing list