Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: Stylistic note on loops

 
 
Ian Collins
Guest
Posts: n/a
 
      10-31-2010
On 11/ 1/10 12:32 AM, Richard wrote:
> James Dow Allen<(E-Mail Removed)> writes:
>
>> "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). 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.


It's a great way to confuse readers!

--
Ian Collins
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      10-31-2010
Nick Keighley <(E-Mail Removed)> writes:
> On 23 Oct, 23:12, Ian Collins <(E-Mail Removed)> wrote:

[...]
>> 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)


There's usually a way to silence warnings about unused parameters, such
as

(void)unused_param;

(But there's no *standard* way to do so.)

--
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
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      10-31-2010
On 10/31/2010 3:04 PM, Keith Thompson wrote:
>
> There's usually a way to silence warnings about unused parameters, such
> as
>
> (void)unused_param;
>
> (But there's no *standard* way to do so.)


There is no *standard* way to silence any warning whatsoever,
including "scary.c: trick or treat!"

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      10-31-2010
Ian Collins <(E-Mail Removed)> 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).

Have you ever tried to speak for someone from whom you know
that he can not understand English very well? You will have
noticed that you language becomes crippled and unnatural.

We must write code that is as readable as possible. But
we may assume that the reader

- knows C and

- reads every single character of the source code,

otherwise writing readable code is impossible (because we
cannot know upfront which specific part of C a specific
reader will not know or which specific part of our source
code he will miss to read).

One exercise of my programming class for C beginners is
to predict the output of this program:

#include <stdio.h>

int main()
{ printf( "1\n" ); /* writes "1" *
printf( "2\n" ); * writes "2" */ }

.


 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      10-31-2010
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Stefan Ram) writes:
>{ printf( "1\n" ); /* writes "1" *
> printf( "2\n" ); * writes "2" */ }


Or, what do you think about the following variant?
(I am not sure if it's still valid and portable C.)

{ printf( "1\n" ); /* writes "1" *\
printf( "2\n" ); \* writes "2" */ }

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      10-31-2010
On 10/31/2010 3:22 PM, Stefan Ram wrote:
> (E-Mail Removed)-berlin.de (Stefan Ram) writes:
>> { printf( "1\n" ); /* writes "1" *
>> printf( "2\n" ); * writes "2" */ }

>
> Or, what do you think about the following variant?
> (I am not sure if it's still valid and portable C.)
>
> { printf( "1\n" ); /* writes "1" *\
> printf( "2\n" ); \* writes "2" */ }


I'm not entirely convinced of the pedagogical value, but
if you think this sort of thing is useful consider:

printf ("2\n"); /*
printf ("3\n"); * Print all the
printf ("5\n"); * 1-digit primes
printf ("7\n"); */

Throw in a red herring, if you wish:

printf ("2\n"); /*
printf ("3\n"); * Print all the
printf ("5\n"); * 1-digit primes
printf ("9\n"); */

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-31-2010
On 11/ 1/10 08:17 AM, Stefan Ram wrote:
> Ian Collins<(E-Mail Removed)> 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 <(E-Mail Removed)> 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?

--
Ian Collins
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-31-2010
On 11/ 1/10 09:48 AM, superpollo wrote:
> Ian Collins ha scritto:
>> On 11/ 1/10 08:17 AM, Stefan Ram wrote:
>>> Ian Collins<(E-Mail Removed)> 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 <(E-Mail Removed)> 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?

>
> and what if sometime in the future you need to meat up a bit the body of
> the loop?


You add some. I don't see the relevance of the question.

--
Ian Collins
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-31-2010
(E-Mail Removed)-berlin.de (Stefan Ram) writes:
> (E-Mail Removed)-berlin.de (Stefan Ram) writes:
>>{ printf( "1\n" ); /* writes "1" *
>> printf( "2\n" ); * writes "2" */ }

>
> Or, what do you think about the following variant?
> (I am not sure if it's still valid and portable C.)
>
> { printf( "1\n" ); /* writes "1" *\
> printf( "2\n" ); \* writes "2" */ }


Yes, it's valid. The backslash on the end of the first line is
a line-splicing character if it's not followed by white space, or
just part of the comment if it is, but the program's meaning is the
same either way. (I had thought it might affect later occurrences
of __LINE__, but the line number is defined in terms of physical
lines, not logical lines.)

--
Keith Thompson (The_Other_Keith) (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
 
Keith Thompson
Guest
Posts: n/a
 
      10-31-2010
Ian Collins <(E-Mail Removed)> writes:
> On 11/ 1/10 08:17 AM, Stefan Ram wrote:
>> Ian Collins<(E-Mail Removed)> 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 <(E-Mail Removed)> 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;
}

--
Keith Thompson (The_Other_Keith) (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
 
 
 
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