Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Stylistic note on loops

Reply
Thread Tools

Stylistic note on loops

 
 
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
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
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-02-2010
Nick <(E-Mail Removed)> writes:

> Thomas Pornin <(E-Mail Removed)> writes:

<snip>
>> 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).

<snip>
> 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));


There should be a second argument in this call, but that's not the point
of your post.

> 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?


No, not in C99. The presence of either a return with an expression in a
void function; or the presence of one without in a non-void function are
constraint violations in C99.

If you remove the first return statement altogether (and thus simply
allow execution to "fall off the end" of the function) then the program
is OK (presuming that there are all the right declarations, of course).

--
Ben.
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      11-02-2010
On Mon, 1 Nov 2010, Thomas Pornin wrote:

> 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.


Thanks - and to Eric - for the explanation.

> Of course, nothing of the above prevents a C compiler from emitting a
> warning about the lack of return.


Nor, presumably, from doing something crafty to cause that usage to blow
up loudly at runtime.

tom

--
Scheme is simple and elegant *if you're a computer*.
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      11-02-2010
On Tue, 2 Nov 2010, 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.?


I can assure you that that sentence is not equivalent to the one without
the expletive. I have CVS records of a week of code cleanup to prove it.

tom

--
Scheme is simple and elegant *if you're a computer*.
 
Reply With Quote
 
Morris Keesan
Guest
Posts: n/a
 
      11-02-2010
On Mon, 01 Nov 2010 23:40:24 -0400, Eric Sosman
<(E-Mail Removed)> wrote:

> On 11/1/2010 9:40 PM, Stefan Ram wrote:
>> Eric Sosman<(E-Mail Removed)> writes:

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

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


By induction, we could shorten this to
"When wordings are equivalent, use the shortest."
--
Morris Keesan -- (E-Mail Removed)
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      11-02-2010
On Nov 2, 6:12*pm, "Morris Keesan" <(E-Mail Removed)> wrote:
> On Mon, 01 Nov 2010 23:40:24 -0400, Eric Sosman *
>
>
>
> <(E-Mail Removed)> wrote:
> > On 11/1/2010 9:40 PM, Stefan Ram wrote:
> >> Eric Sosman<(E-Mail Removed)> *writes:

> ...
> >> * * * *When two wordings are equivalent, use the shorter one.

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

>
> > * * *

>
> By induction, we could shorten this to
> * * "When wordings are equivalent, use the shortest."
> --
> Morris Keesan -- (E-Mail Removed)


Use the shortest of equivalent wordings.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-02-2010
Malcolm McLean <(E-Mail Removed)> writes:
> On Nov 2, 6:12*pm, "Morris Keesan" <(E-Mail Removed)> wrote:
>> On Mon, 01 Nov 2010 23:40:24 -0400, Eric Sosman *
>>
>> <(E-Mail Removed)> wrote:
>> > On 11/1/2010 9:40 PM, Stefan Ram wrote:
>> >> Eric Sosman<(E-Mail Removed)> *writes:

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

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

>>
>> > * * *

>>
>> By induction, we could shorten this to
>> * * "When wordings are equivalent, use the shortest."
>> --
>> Morris Keesan -- (E-Mail Removed)

>
> Use the shortest of equivalent wordings.


Wrdngs equiv? Shrtst!

--
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
Re: Stylistic note on loops Stefan Ram C Programming 161 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