Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: Stylistic note on loops

 
 
Tom Anderson
Guest
Posts: n/a
 
      11-01-2010
On Sun, 31 Oct 2010, Stefan Ram wrote:

> 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" */ }


Crafty!

But will it even compile, given the lack of a return statement in a
non-void-returning method?

Maybe it will, being C. It bloody well shouldn't though!

tom

--
Through the darkness of Future Past the magician longs to see.
 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      11-01-2010
"Tom Anderson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) th.li...

> Python has been called 'executable pseudocode'. If written cleanly, it is
> highly readable, even to those who don't know it.


I think Python is the last language I'd use for pseudocode. No-one writing
Python can resist using all it's features which renders it incomprehensible
to anyone else. And for porting code, it is also necessary to duplicate it's
myriad libraries.

Probably Lua is better for that role, being far simpler with less built-in
stuff.

--
Bartc

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      11-02-2010
On 11/1/2010 5:05 PM, Tom Anderson wrote:
> On Sun, 31 Oct 2010, Stefan Ram wrote:
>
>> 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" */ }

>
> Crafty!
>
> But will it even compile, given the lack of a return statement in a
> non-void-returning method?
>
> Maybe it will, being C. It bloody well shouldn't though!


Under C90 rules it will compile, but the effect is undefined
(unless the environment chooses to ignore the returned value).
Under C99 rules it will compile, and will return zero.

I agree heartily with your final sentence.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      11-02-2010
Eric Sosman <(E-Mail Removed)> writes:
>>It bloody well shouldn't though!

>I agree heartily with your final sentence.


By this you agree with a sentence with an expletive, but
whatever: This reminds my of one of my own style rules, that
I can apply to both topics discussed here: loops and the
main function:

»When two wordings are equivalent, use the shorter one.«

Thus, »; while( example() );«, not »; while( example() ){}«,
and »int main(){}«, not »int main(){ return 0; }«.

(A semicolon was added above in front of the »while«
to clarify that this is not the tail of a do loop.)

This rule, however does not apply to formatting (white space),
so »{ return 0; }« is ok, even if »{return 0;}« would be
shorter.

In the case of »while( 1 )« versus »for( ;; )« I am not sure:
The for loop is shorter in terms of non-whitespace characters
while the while loop is shorter in terms of tokens.

 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      11-02-2010
Patricia Shanahan <(E-Mail Removed)> writes:
>Do you consider this rule a way of making the code as readable as
>possible, or a substitute for that approach?


Possibly, we sometimes make terms absolute, which really
need a referent. »Readable« might be a predicate of a text
and a reader:

Readable( text, reader ),

not of a text alone

Readable( text ).

Having said this, I see this rule as a rule that can coexist
with a rule requiring readability and maintainability but in
parts has a different scope.

It is much more precise. While one man's readability could
be another man's obfuscation, the lenght of a code segment
is more objective.

Usually, any unnecessary part will catch the attention of
the reader and might disctract him or make him think about
something irrelevant (that is, »why was this verbose/
unnecessary part added here?«).

For example, we have:

cos 2x = cos² x - sin² x

If this is written as

cos 2x =( cos² x )-( sin² x ),

, one man might say »Great! Now I finally see the
precedences - that 's more readable!«, another man might say
»Well, what does the author want to convey with the
additional redundant braces?«. So, it might be difficult to
be sure what is »more readable« of the two. In such case my
brevity rule helps to find a »canonical« form, that is, to
prefer the first one.

But I see my rule more in terms of token counts than in
terms of letter counts, so I do not want to oppose variable
names to be sufficiently long to carry enough meaning to
help the reader to understand the code.

I would say that, when it is obvious what is more readable,
the more readable variant should be chosen and would restrict
my brevity rule to a subordinate rôle, that is, only when
the readability of two choice does not differ that much or
can not be determined precisely enough to make a clear choice,
the I would go for the shorter one.

It should have become clear that I do not see any problem
whatsoever to read a loop body made of a semicolon statement
solely or a main function implicitly returning a value, so
in these cases, I do not hesitate to apply my brevity rul
rule. I would not apply it, should it's result look
obviously less readable to me, because in this case,
readability would be more important than brevity.

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-02-2010
On 11/1/2010 9:40 PM, Stefan Ram wrote:
> Eric Sosman<(E-Mail Removed)> writes:
>>> It bloody well shouldn't though!

>> I agree heartily with your final sentence.

>
> By this you agree with a sentence with an expletive, but
> whatever: This reminds my of one of my own style rules, that
> I can apply to both topics discussed here: loops and the
> main function:
>
> »When two wordings are equivalent, use the shorter one.«


ITYM "When two wordings are equivalent, use the shorter."



--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-02-2010
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Stefan Ram) writes:

> Eric Sosman <(E-Mail Removed)> writes:
>>>It bloody well shouldn't though!

>>I agree heartily with your final sentence.

>
> By this you agree with a sentence with an expletive, but
> whatever: This reminds my of one of my own style rules, that
> I can apply to both topics discussed here: loops and the
> main function:
>
> »When two wordings are equivalent, use the shorter one.«


This just shifts all the burden onto the word "equivalent". If one form
is clearer than another, are they equivalent? Surely you can't mean
simply semantic equivalence?

> Thus, »; while( example() );«, not »; while( example() ){}«,
> and »int main(){}«, not »int main(){ return 0; }«.


I won't comment on the style since discussions of that sort are so often
unproductive.

> (A semicolon was added above in front of the »while«
> to clarify that this is not the tail of a do loop.)


But it may still be the tail of a do loop! If you really must show that
the while is at the start of a statement, you could use a label:

x: while (example());

or, I think, you could write:

) while (example());

or

else while (example());

<snip>
--
Ben.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-02-2010
Stefan Ram wrote:
>> »When two wordings are equivalent, use the shorter one.«


Eric Sosman wrote:
> ITYM "When two wordings are equivalent, use the shorter."
>
>


Simplify! Simplify! Simplify!
- Henry David Thoreau

One simplify would have sufficed.
- Ralph Waldo Emerson

However, what may be a good rule for code (and I am not saying it is) does not
make a good rule for literature. Verbosity is a tool of the literate every
bit as much as brevity.

One "simplify" may well have sufficed, or perhaps Thoreau intended the
self-contradiction, or perhaps was setting up Emerson for the reply. (They
were good friends and in the habit of trading quips.) For the latter two
purposes, one "simplify" would not have sufficed.

It's all accordin'. One may write

for ( int len; (len = read( buf )) > 0; output( buf, len ) )
{
}

with intentional bracely verbosity specifically to call attention to the empty
loopness of the construct, granting the equivalence in readability but
asserting the need for emphasis, in which case one semicolon would not suffice.

--
Lew
"Je n'ai fait celle-ci plus longue que parce que je n'ai pas eu le loisir de
la faire plus courte."
Blaise Pascal, 1623-1662
 
Reply With Quote
 
Nick
Guest
Posts: n/a
 
      11-02-2010
Thomas Pornin <(E-Mail Removed)> writes:

> According to Tom Anderson <(E-Mail Removed)>:
>> But will it even compile, given the lack of a return statement in a
>> non-void-returning method?

>
> In C, the lack of a return statement in a non-void-returning method is
> allowed. What triggers "undefined behaviour" (aka "all of Hell is
> breaking loose") is reaching the closing '}' of a function _and_ trying
> to use the returned value (or, more appropriately, the value which
> should have been returned but was not). In a C implementation with a
> well-defined compilation phase (that's the normal state of affairs in
> C), the compilation MUST NOT fail on such a lack of a return statement
> in a non-void-returning function: problems implied by that lack of
> return may become apparent only at runtime.


I've often wondered - although I'd never write it - about something like
this:

int process_items(struct thing *items, int mode) {
if(mode == 0) {
/* do some stuff */
return;
} else {
int count = 0;
/* do some other stuff, counting items */
return count;
}
}

int main(void) {
struct thing *stuff = create_items();
/* set up items etc ... */
process_items(stuff,1);
printf("There are %d\n",process_items(stuff));
return EXIT_SUCCESS;
}

As I said, lots of reasons not to do it that way - including that this
toy is clearly a case for a single return and a function-wide count variable that
is always returned - but is it entirely OK?

> C is not for the faint of heart. It probably isn't for the sane of
> brain either.

--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk
 
Reply With Quote
 
ClassCastException
Guest
Posts: n/a
 
      11-02-2010
On Tue, 02 Nov 2010 00:17:13 -0400, Lew wrote:

> Stefan Ram wrote:
>>> »When two wordings are equivalent, use the shorter one.«

>
> Eric Sosman wrote:
>> ITYM "When two wordings are equivalent, use the shorter."
>>
>>

>
> Simplify! Simplify! Simplify!
> - Henry David Thoreau
>
> One simplify would have sufficed.
> - Ralph Waldo Emerson
>
> However, what may be a good rule for code (and I am not saying it is)
> does not make a good rule for literature. Verbosity is a tool of the
> literate every bit as much as brevity.
>
> One "simplify" may well have sufficed, or perhaps Thoreau intended the
> self-contradiction, or perhaps was setting up Emerson for the reply.
> (They were good friends and in the habit of trading quips.) For the
> latter two purposes, one "simplify" would not have sufficed.
>
> It's all accordin'. One may write
>
> for ( int len; (len = read( buf )) > 0; output( buf, len ) ) {
> }
>
> with intentional bracely verbosity specifically to call attention to the
> empty loopness of the construct, granting the equivalence in readability
> but asserting the need for emphasis, in which case one semicolon would
> not suffice.


Emphasis also provides a fourth reason for tripling the "Simplify!".

 
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