Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: Stylistic note on loops (http://www.velocityreviews.com/forums/t736229-re-stylistic-note-on-loops.html)

Stefan Ram 10-23-2010 12:36 AM

Re: Stylistic note on loops
 
Eric Sosman <esosman@ieee-dot-org.invalid> writes:
>On 10/21/2010 7:40 PM, Stefan Ram wrote:
>>In C, to skip two space-separated integral numerals
>>(under certain circumstances):
>>while( isnum( *++c )); while( isspace( *++c )); while( isnum( *++c ));

>(...) Personally, I cannot recall a single occasion in more than forty
>years of programming when I (1) wanted to skip over two numbers this
>way and (2) wanted to ignore what those numbers were and (3) had such


I have been looking for similar loops »in the wild«:

A sequence of skipping scanning while loops:

while (!isspace(*p)) ++p; while (isspace(*p)) ++p;

http://github.com/mono/mono/blob/master/libgc/os_dep.c

While loops with a semicolon (and no corresponding »do« in front of them):

while (*c && *c++!='\n');

http://www.opensource.apple.com/sour...tml/man2html.c

Well, and I believe:

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

is an idiom known to every C programmer.


BGB / cr88192 10-23-2010 04:39 PM

Re: Stylistic note on loops
 

"christian.bau" <christian.bau@cbau.wanadoo.co.uk> wrote in message
news:493ce92e-97c0-47e2-8713-2b0ab384e517@x42g2000yqx.googlegroups.com...
On Oct 23, 1:36 am, r...@zedat.fu-berlin.de (Stefan Ram) wrote:

> Well, and I believe:
>
> while( *p++ = *q++ );
>
> is an idiom known to every C programmer.


<--
Many compilers will give you a warning for the ";" immediately
following the closing parenthesis without at least white space in
between - likelyhood that the semicolon is there by mistake is too
high. For example

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

is surely a bug.
-->

not that I have seen...


in most C and C++ code this practice is sufficiently common that it would
make a lot of code start spewing warnings all over the place if compilers
were to warn about it.

but, then again, some compilers will warn about unused local variables,
which is personally a bit annoying.
hence, it becomes fairly common to ignore many warning conditions until they
become errors if the compiler is intent on giving too many warnings over
pointless crap and one doesn't feel like spending all the extra time needed
to go clean them up...

it is better when warnings are confined to more serious issues, like using
an uninitialized variable, ...


warning: implicit conversion from 'void *' to 'Foo *' without a cast; ...
hence the dreaded "casting the result of malloc" event (some people get
really fussy for or against casting the results of malloc calls...).


admittedly, using a while-loop to perform a copy or similar this way is
generally ugly (usually strcpy or memcpy is preferable, and often faster).

much like, 'strcmp()' is the usual way of copying strings, and is generally
preferable to hand-rolled loops.

but, there are many other cases where one will end up using loops like this.




BartC 10-23-2010 10:20 PM

Re: Stylistic note on loops
 

"Keith Thompson" <kst-u@mib.org> wrote in message
news:lnd3r0y5r0.fsf@nuthaus.mib.org...

> "BGB / cr88192" <cr88192@hotmail.com> writes:
> [...]
>> but, then again, some compilers will warn about unused local variables,
>> which is personally a bit annoying.


> You find warnings about unused local variable annoying? Why?
> Is there any reason to declare a variable if you're not going to
> use it?


Where code is incomplete, or in development, declarations can exist but not
the statements that use them (or they might be commented out temporarily).

For example, if I've just written a complicated set of declarations, I might
compile to check for errors, before I've written any actual code.

This sort of warning is useful when development has finished.

--
Bartc




Keith Thompson 10-23-2010 10:55 PM

Re: Stylistic note on loops
 
"BartC" <bc@freeuk.com> writes:
> "Keith Thompson" <kst-u@mib.org> wrote in message
> news:lnd3r0y5r0.fsf@nuthaus.mib.org...
>> "BGB / cr88192" <cr88192@hotmail.com> writes:
>> [...]
>>> but, then again, some compilers will warn about unused local variables,
>>> which is personally a bit annoying.

>
>> You find warnings about unused local variable annoying? Why?
>> Is there any reason to declare a variable if you're not going to
>> use it?

>
> Where code is incomplete, or in development, declarations can exist but not
> the statements that use them (or they might be commented out temporarily).
>
> For example, if I've just written a complicated set of declarations, I might
> compile to check for errors, before I've written any actual code.
>
> This sort of warning is useful when development has finished.


Or during maintenance, which typically continues long after development
has "finished".

Personally, I want to be warned about unused variables unless I
ask not to be. Most compilers let you disable specific warnings.

--
Keith Thompson (The_Other_Keith) kst-u@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"

Ian Collins 10-23-2010 11:12 PM

Re: Stylistic note on loops
 
On 10/24/10 11:20 AM, BartC wrote:
>
> "Keith Thompson" <kst-u@mib.org> wrote in message
> news:lnd3r0y5r0.fsf@nuthaus.mib.org...
>
>> "BGB / cr88192" <cr88192@hotmail.com> writes:
>> [...]
>>> but, then again, some compilers will warn about unused local variables,
>>> which is personally a bit annoying.

>
>> You find warnings about unused local variable annoying? Why?
>> Is there any reason to declare a variable if you're not going to
>> use it?

>
> Where code is incomplete, or in development, declarations can exist but
> not the statements that use them (or they might be commented out
> temporarily).
>
> For example, if I've just written a complicated set of declarations, I
> might compile to check for errors, before I've written any actual code.


If you are doing that, your functions are too long or complex. If the
code as written does not require a variable, don't declare it.

--
Ian Collins

James Dow Allen 10-31-2010 07:40 AM

Re: Stylistic note on loops
 
"BGB / cr88192" <cr88192@hotmail.com> might have writ, in
news:i9v341$2so$1@news.albasani.net:

> ... some compilers will warn about unused local
> variables, which is personally a bit annoying.


I once used a "language" where some unused variables, believe it or not,
caused Fatal Errors!! I even complained about it on Usenet in
Message <90507@sun.uucp> :

http://groups.google.com/group/comp....4732a57ef112d?
hl=en&dmode=source

(People e-mailed me in response to that post, saying they'd printed
it and taped it to the wall!)

> "christian.bau" <christian.bau@cbau.wanadoo.co.uk> wrote:
>> while( *p++ = *q++ );
>>
>> is an idiom known to every C programmer.


I prefer
while (*p++ = *q++) {
}

James Dow Allen

Malcolm McLean 10-31-2010 12:02 PM

Re: Stylistic note on loops
 
On Oct 23, 6:39*pm, "BGB / cr88192" <cr88...@hotmail.com> wrote:
> "christian.bau" <christian....@cbau.wanadoo.co.uk> wrote in message
>
> but, there are many other cases where one will end up using loops like this.
>

for(i=0;i<N;i++)
/* do work here */

or

for(i=0;array[i] != sentinel;i++)
/* do work here */

is my preferred method, because it doesn't corrupt the pointers. In
the olden days indexing used to be a little bit slower because the
compiler would create an extra variable and offset calculation.
However that's no longer much of a concern.


Nick Keighley 10-31-2010 01:54 PM

Re: Stylistic note on loops
 
On 23 Oct, 23:12, Ian Collins <ian-n...@hotmail.com> wrote:
> On 10/24/10 11:20 AM, BartC wrote:
>
>
>
>
>
>
>
> > "Keith Thompson" <ks...@mib.org> wrote in message
> >news:lnd3r0y5r0.fsf@nuthaus.mib.org...

>
> >> "BGB / cr88192" <cr88...@hotmail.com> writes:
> >> [...]
> >>> but, then again, some compilers will warn about unused local variables,
> >>> which is personally a bit annoying.

>
> >> You find warnings about unused local variable annoying? Why?
> >> Is there any reason to declare a variable if you're not going to
> >> use it?

>
> > Where code is incomplete, or in development, declarations can exist but
> > not the statements that use them (or they might be commented out
> > temporarily).

>
> > For example, if I've just written a complicated set of declarations, I
> > might compile to check for errors, before I've written any actual code.

>
> If you are doing that, your functions are too long or complex. *If the
> code as written does not require a variable, don't declare it.


sometimes you're forced to declare paramters you don't use (eg.
callback functions)

Donkey Hottie 10-31-2010 06:13 PM

Re: Stylistic note on loops
 
On 31.10.2010 13:32, Richard wrote:
> James Dow Allen <gmail@jamesdowallen.nospam> writes:
>>> "christian.bau" <christian.bau@cbau.wanadoo.co.uk> wrote:
>>>> while( *p++ = *q++ );
>>>>
>>>> is an idiom known to every C programmer.

>>
>> I prefer
>> while (*p++ = *q++) {
>> }
>>
>> James Dow Allen

>
> Why? That's ridiculous.
>
> The point of having no {} is that you realise that all the *serious*
> stuff is going on inside the while loop statement itself.


I prefer

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

The empty statement where the single statement always goes. Can not be
seen as a typo.

--

Don't relax! It's only your tension that's holding you together.

Ian Collins 10-31-2010 06:56 PM

Re: Stylistic note on loops
 
On 11/ 1/10 02:54 AM, Nick Keighley wrote:
> On 23 Oct, 23:12, Ian Collins<ian-n...@hotmail.com> wrote:
>> On 10/24/10 11:20 AM, BartC wrote:
>>
>>
>>
>>
>>
>>
>>
>>> "Keith Thompson"<ks...@mib.org> wrote in message
>>> news:lnd3r0y5r0.fsf@nuthaus.mib.org...

>>
>>>> "BGB / cr88192"<cr88...@hotmail.com> writes:
>>>> [...]
>>>>> but, then again, some compilers will warn about unused local variables,
>>>>> which is personally a bit annoying.

>>
>>>> You find warnings about unused local variable annoying? Why?
>>>> Is there any reason to declare a variable if you're not going to
>>>> use it?

>>
>>> Where code is incomplete, or in development, declarations can exist but
>>> not the statements that use them (or they might be commented out
>>> temporarily).

>>
>>> For example, if I've just written a complicated set of declarations, I
>>> might compile to check for errors, before I've written any actual code.

>>
>> If you are doing that, your functions are too long or complex. If the
>> code as written does not require a variable, don't declare it.

>
> sometimes you're forced to declare paramters you don't use (eg.
> callback functions)


The topic was unused local variables, not unused parameters.

--
Ian Collins


All times are GMT. The time now is 01:28 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.