Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Stylistic note on loops

Reply
Thread Tools

Re: Stylistic note on loops

 
 
Stefan Ram
Guest
Posts: n/a
 
      10-23-2010
Eric Sosman <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
BGB / cr88192
Guest
Posts: n/a
 
      10-23-2010

"christian.bau" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On Oct 23, 1:36 am, (E-Mail Removed)-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.



 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      10-23-2010

"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> "BGB / cr88192" <(E-Mail Removed)> 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



 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-23-2010
"BartC" <(E-Mail Removed)> writes:
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> "BGB / cr88192" <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-23-2010
On 10/24/10 11:20 AM, BartC wrote:
>
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> "BGB / cr88192" <(E-Mail Removed)> 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
 
Reply With Quote
 
James Dow Allen
Guest
Posts: n/a
 
      10-31-2010
"BGB / cr88192" <(E-Mail Removed)> might have writ, in
news:i9v341$2so$(E-Mail Removed):

> ... 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 <(E-Mail Removed)> :

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" <(E-Mail Removed)> wrote:
>> while( *p++ = *q++ );
>>
>> is an idiom known to every C programmer.


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

James Dow Allen
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      10-31-2010
On Oct 23, 6:39*pm, "BGB / cr88192" <(E-Mail Removed)> wrote:
> "christian.bau" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      10-31-2010
On 23 Oct, 23:12, Ian Collins <(E-Mail Removed)> wrote:
> On 10/24/10 11:20 AM, BartC wrote:
>
>
>
>
>
>
>
> > "Keith Thompson" <(E-Mail Removed)> wrote in message
> >news:(E-Mail Removed)...

>
> >> "BGB / cr88192" <(E-Mail Removed)> 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)
 
Reply With Quote
 
Donkey Hottie
Guest
Posts: n/a
 
      10-31-2010
On 31.10.2010 13:32, Richard wrote:
> James Dow Allen <(E-Mail Removed)> writes:
>>> "christian.bau" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-31-2010
On 11/ 1/10 02:54 AM, Nick Keighley wrote:
> On 23 Oct, 23:12, Ian Collins<(E-Mail Removed)> wrote:
>> On 10/24/10 11:20 AM, BartC wrote:
>>
>>
>>
>>
>>
>>
>>
>>> "Keith Thompson"<(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...

>>
>>>> "BGB / cr88192"<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stylistic note on loops markspace Java 221 11-21-2010 02:08 PM
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM
Stylistic question -- initialization lists and vectors Denis Remezov C++ 4 04-30-2004 05:10 PM
stylistic use ofexplicit this. P.Hill Java 13 04-23-2004 04:21 AM
stylistic prototype question Mantorok Redgormor C Programming 5 10-30-2003 01:51 PM



Advertisments