Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 12 Feb 2005 00:49:03 +1100 (EST)
From:      Bruce Evans <bde@zeta.org.au>
To:        Poul-Henning Kamp <phk@phk.freebsd.dk>
Cc:        cvs-src@freebsd.org
Subject:   Re: cvs commit: src/sys/dev/ed if_ed.c 
Message-ID:  <20050212001652.L32719@delplex.bde.org>
In-Reply-To: <10137.1108121831@critter.freebsd.dk>
References:  <10137.1108121831@critter.freebsd.dk>

next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, 11 Feb 2005, Poul-Henning Kamp wrote:

> In message <20050211220212.A32327@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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050212001652.L32719>