Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Stylistic note on loops

Path: g2news2.google.com!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!feeder.news-service.com!85.214.198.2.MISMATCH!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From: Keith Thompson <ks...@mib.org>
Newsgroups: comp.lang.java.programmer,comp.lang.c
Subject: Re: Stylistic note on loops
Date: Fri, 05 Nov 2010 16:58:32 -0700
Organization: None to speak of
Lines: 68
Message-ID: <lnlj57cogn.fsf@nuthaus.mib.org>
References: <i9ptik$rcd$1@news.eternal-september.org>
	<yOydneJouKzoXF3RnZ2dnUVZ_q2dnZ2d@earthlink.com>
	<style-20101022011700@ram.dialup.fu-berlin.de>
	<semicolon-20101022013931@ram.dialup.fu-berlin.de>
	<i9r03t$ppg$1@news.eternal-september.org>
	<loops-20101023023312@ram.dialup.fu-berlin.de>
	<493ce92e-97c0-47e2-8713-2b0ab384e517@x42g2000yqx.googlegroups.com>
	<i9v341$2so$1@news.albasani.net>
	<Xns9E229549284CCjamesdowallen@78.46.73.112>
	<iajk3j$vj8$1@news.eternal-september.org>
	<8j5sjtF51nU2@mid.individual.net>
	<C-20101031200839@ram.dialup.fu-berlin.de>
	<8j61r6F51sU1@mid.individual.net> <lnlj5em26m.fsf@nuthaus.mib.org>
	<23edfcb4-4d39-4713-a351-1cb20ad3fa17@h21g2000vbh.googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: mx01.eternal-september.org; posting-host="9nFTuw1iclElqUG5+//YDQ";
	logging-data="16661"; mail-complaints-to="ab...@eternal-september.org";	posting-account="U2FsdGVkX1+BOlpsgfxrT/WXP7NdR9bv"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)
Cancel-Lock: sha1:2KmgbxVl+s33RML/Nm81y07efMQ=
	sha1:6rV8DczrnHCNAvtXhtyzEzb3mA8=

"Thomas G. Marshall" <tgmfo...@gmail.com> writes:
> On Oct 31, 6:23 pm, Keith Thompson <ks...@mib.org> wrote:
>> Ian Collins <ian-n...@hotmail.com> writes:
>> > On 11/ 1/10 08:17 AM, Stefan Ram wrote:
>> >> Ian Collins<ian-n...@hotmail.com>  writes:
>> >>>> The point of having no {} is that you realise that all the *serious*
>> >>>> stuff is going on inside the while loop statement itself.
>> >>> It's a great way to confuse readers!
>>
>> >>    Indeed, most people will be confused by this, since most
>> >>    people do not know the C programming language at all.
>> >>    However, a C programmer will not be confused by this (by
>> >>    definition).
>>
>> > Restoring the context:
>>
>> > James Dow Allen <gm...@jamesdowallen.nospam> writes:
>>
>> >  > I prefer
>> >  >     while (*p++ = *q++) {
>> >  >     }
>>
>> > The confusion stems not from the syntax, but from the empty braces, did
>> > he intend there to be some code in there?
>>
>> One possible alternative is:
>>
>>     while (*p++ = *q++) {
>>         /* nothing */
>>     }
>>
>> Another is:
>>
>>     while (*p++ = *q++) {
>>         continue;
>>     }
>
> I know you weren't specifically suggesting these per se, but I might
> argue that both of those are a form of over-engineering that I don't
> think there's a term for.  Over verbosity?  While they certainly seem
> safer overall, it somehow seems just a little more than is necessary
> for even the exhausted engineer at 3am hopped on caffeine <--
> (tiredness & caffeine-addled are my standard metrics for defensive
> coding).

I wasn't specifically suggesting them, but now I will: I specifically
suggest them.  I don't find either form overly verbose.

In this particular case, I'd say that a loop with an empty body is
sufficiently unusual that there should be *some* explicit way of
indicating it; otherwise it's easily mistaken for a typo.

For example, suppose you see this:

    while (*p++ = *q++);
        do_something;

Clearly the code is either incorrect or poorly formatted -- but which?
Maybe the author's tab stop settings just don't match yours.

YMMV, and apparently it does.  I'm aware that my personal taste in
C coding style is more verbose than the average.

-- 
Keith Thompson (The_Other_Keith) ks...@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"