Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #150711 > unrolled thread

Comma operator in for() statements

Started byGeoff <geoff@invalid.invalid>
First post2020-03-21 19:51 -0700
Last post2020-03-27 08:02 -0700
Articles 11 on this page of 111 — 20 participants

Back to article view | Back to comp.lang.c


Contents

  Comma operator in for() statements Geoff <geoff@invalid.invalid> - 2020-03-21 19:51 -0700
    Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-22 03:31 +0000
      Re: Comma operator in for() statements Geoff <geoff@invalid.invalid> - 2020-03-21 23:17 -0700
        Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-22 12:00 +0000
          Re: Comma operator in for() statements Geoff <geoff@invalid.invalid> - 2020-03-22 08:57 -0700
            Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-22 16:20 +0000
              Re: Comma operator in for() statements Geoff <geoff@invalid.invalid> - 2020-03-22 14:02 -0700
                Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-22 21:26 +0000
      Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-22 10:25 +0100
      Re: Comma operator in for() statements Richard Damon <Richard@Damon-Family.org> - 2020-03-22 08:27 -0400
        Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-22 16:31 +0000
    Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-22 00:33 -0400
    Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-22 12:23 +0000
      Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-22 05:47 -0700
        Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-22 14:17 +0000
          Re: Comma operator in for() statements felix@palmen-it.de (Felix Palmen) - 2020-03-22 15:55 +0100
            Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-22 15:01 +0000
              Re: Comma operator in for() statements felix@palmen-it.de (Felix Palmen) - 2020-03-22 16:05 +0100
                Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-22 16:46 +0000
                  Re: Comma operator in for() statements felix@palmen-it.de (Felix Palmen) - 2020-03-22 18:14 +0100
                  Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-22 11:42 -0700
                    Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-22 22:03 +0000
                      Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-22 23:51 -0700
                        Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-23 14:37 +0000
                          Re: Comma operator in for() statements Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-23 08:41 -0700
                            Re: Comma operator in for() statements Guillaume <message@bottle.org> - 2020-03-23 16:53 +0100
                            Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-23 16:31 +0000
                              Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-23 16:48 +0000
                                Re: Comma operator in for() statements Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-23 09:53 -0700
                                  Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-23 18:59 +0100
                                    Re: Comma operator in for() statements Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-23 11:23 -0700
                                      Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-23 19:51 +0000
                                        Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-23 14:28 -0700
                                      Re: Comma operator in for() statements Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-23 13:09 -0700
                                        Re: Comma operator in for() statements Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-23 13:20 -0700
                                          Re: Comma operator in for() statements scott@slp53.sl.home (Scott Lurndal) - 2020-03-23 20:26 +0000
                                          Re: Comma operator in for() statements Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-23 13:55 -0700
                                            Re: Comma operator in for() statements Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-23 15:43 -0700
                                              Re: Comma operator in for() statements Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-23 16:27 -0700
                                                Re: Comma operator in for() statements Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-23 17:13 -0700
                                                  Re: Comma operator in for() statements Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-23 21:11 -0700
                                                Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-24 00:19 +0000
                                                Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-24 10:30 +0100
                                                  Re: Comma operator in for() statements Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-24 03:07 -0700
                                                    Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-24 11:37 +0100
                                                  Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-24 11:18 +0000
                                                    Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-24 13:23 +0100
                                                      Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-24 14:53 +0000
                                                        Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-25 10:35 +0100
                                                          Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-25 11:12 +0000
                                                      Re: Comma operator in for() statements Ike Naar <ike@sdf.lonestar.org> - 2020-03-26 19:21 +0000
                                                    Re: Comma operator in for() statements luser droog <luser.droog@gmail.com> - 2020-03-24 21:22 -0700
                                                      Re: Comma operator in for() statements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-03-27 06:46 -0700
                                                        Re: Comma operator in for() statements luser droog <luser.droog@gmail.com> - 2020-03-28 01:01 -0700
                                                          Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-28 11:26 +0000
                                                            Re: Comma operator in for() statements luser droog <luser.droog@gmail.com> - 2020-03-28 14:22 -0700
                                                              Re: Comma operator in for() statements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-04-02 08:12 -0700
                                                                Re: Comma operator in for() statements luser droog <luser.droog@gmail.com> - 2020-04-02 12:21 -0700
                                                                  Re: Comma operator in for() statements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-04-02 19:43 -0700
                                                  Re: Comma operator in for() statements Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-24 12:56 -0700
                                                  Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-24 23:44 +0000
                                              Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-23 17:10 -0700
                                          Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-24 10:17 +0100
                                  Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-23 18:13 +0000
                          Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-23 09:32 -0700
                            Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-23 19:06 +0100
                          Re: Comma operator in for() statements Richard Damon <Richard@Damon-Family.org> - 2020-03-23 13:23 -0400
                            Re: Comma operator in for() statements Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-23 10:33 -0700
                              Re: Comma operator in for() statements Guillaume <message@bottle.org> - 2020-03-23 18:34 +0100
                              Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-23 18:18 +0000
                          Re: Comma operator in for() statements Ike Naar <ike@sdf.lonestar.org> - 2020-03-23 22:29 +0000
                            Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-23 23:13 +0000
              Re: Comma operator in for() statements richard@cogsci.ed.ac.uk (Richard Tobin) - 2020-03-22 15:17 +0000
            Re: Comma operator in for() statements nobody@example.org (Scott) - 2020-03-22 16:16 +0000
              Re: Comma operator in for() statements Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-23 07:22 -0700
              Re: Comma operator in for() statements Guillaume <message@bottle.org> - 2020-03-25 15:10 +0100
                Re: Comma operator in for() statements Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-03-25 15:18 +0000
            Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-22 16:27 +0000
            Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-22 15:07 -0400
          Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-22 11:33 -0700
        Re: Comma operator in for() statements Anton Shepelev <anton.txt@g{oogle}mail.com> - 2020-03-24 20:25 +0300
          Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-24 23:25 +0000
            Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-25 01:55 +0000
              Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-25 11:01 +0000
            Re: Comma operator in for() statements Anton Shepelev <anton.txt@g{oogle}mail.com> - 2020-03-25 14:59 +0300
              Re: Comma operator in for() statements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-03-27 07:42 -0700
                Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-27 14:51 +0000
                  Re: Comma operator in for() statements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-04-02 19:20 -0700
          Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-24 20:01 -0400
            Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-25 02:05 +0000
              Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-25 15:13 +0000
            Re: Comma operator in for() statements Anton Shepelev <anton.txt@g{oogle}mail.com> - 2020-03-25 15:05 +0300
              Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-25 10:04 -0400
      Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-22 14:49 +0100
    Re: Comma operator in for() statements Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2020-03-22 09:24 -0600
      Re: Comma operator in for() statements Geoff <geoff@invalid.invalid> - 2020-03-22 14:51 -0700
        Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-22 20:13 -0400
          Re: Comma operator in for() statements Geoff <geoff@invalid.invalid> - 2020-03-23 12:16 -0700
            Re: Comma operator in for() statements Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-23 13:16 -0700
            Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-23 17:16 -0400
              Re: Comma operator in for() statements scott@slp53.sl.home (Scott Lurndal) - 2020-03-23 22:40 +0000
    Re: Comma operator in for() statements Guillaume <message@bottle.org> - 2020-03-22 17:31 +0100
      Re: Comma operator in for() statements Geoff <geoff@invalid.invalid> - 2020-03-22 12:28 -0700
        Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-22 15:35 -0400
          Re: Comma operator in for() statements Geoff <geoff@invalid.invalid> - 2020-03-22 13:43 -0700
    Re: Comma operator in for() statements Les Cargill <lcargill99@comcast.com> - 2020-03-22 13:50 -0500
      Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-22 19:25 +0000
        Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-22 21:52 +0100
          Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-22 21:15 +0000
            Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-23 09:17 +0100
    Re: Comma operator in for() statements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-03-27 08:02 -0700

Page 6 of 6 — ← Prev page 1 2 3 4 5 [6]


#150784

Fromscott@slp53.sl.home (Scott Lurndal)
Date2020-03-23 22:40 +0000
Message-ID<GVaeG.110361$2Y3.91774@fx29.iad>
In reply to#150781
James Kuyper <jameskuyper@alumni.caltech.edu> writes:
>On 3/23/20 3:16 PM, Geoff wrote:
>> On Sun, 22 Mar 2020 20:13:17 -0400, James Kuyper
>> <jameskuyper@alumni.caltech.edu> wrote:
>> 
>>> On 3/22/20 5:51 PM, Geoff wrote:
>...
>>>> In retrospect my instinct was wrong and the effect of the comma
>>>> operators is such that my refactor was the opposite of the desired
>>>> effects of the original author. Left of the first comma is the
>>>> initializer of count, right of it is initializer of ent, followed by
>>>> chaining of ent and the count increment. 
>>>
>>> You still seem to be under that there is some significant issue here
>>> involving the comma operator. Do you understand that it would have had
>>> exactly the same behavior if it had been written:
>>>
>>>    for (ent = master, count = 0; ent; ent = count++, ent->chain)
>>>        ;
>>>
>>> ?
>>> If any of those variables had been declared 'volatile' or in
>>> multi-threaded code, that re-write could in principle make a difference
>>> - but it shouldn't matter in any ordinary context.
>> 
>> James,
>> Your rewrite of the line is worse than mine. ent is of type edict_t*
>> and you have assigned an int value to it. Logically incorrect and not
>> equivalent to the original code.
>
>That was a typo. Sorry! It was supposed to be
>
>    for (ent = master, count = 0; ent; count++, ent = ent->chain)
>       ;

although
       for(ent = masterp, count = 0; ent; ent = (count++, ent->chain)) ;

would have worked fine.

[toc] | [prev] | [next] | [standalone]


#150732

FromGuillaume <message@bottle.org>
Date2020-03-22 17:31 +0100
Message-ID<r583sd$hbu$1@gioia.aioe.org>
In reply to#150711
Le 22/03/2020 à 03:51, Geoff a écrit :
> I'm aware of the hazards of using the comma operator in the tested
> expression but I ran across this example and I think it should be
> refactored, but how?
> 
> 	for (count = 0, ent = master; ent; ent = ent->chain, count++)
> 				;

There is no comma operator here in the "tested expression".

> My first instinct is to write it:
> 
> 		for (count = 0; ent; count++)
> 		{
> 			ent = master;
> 			ent = ent->chain;
> 		}
> 
> But is this correct?

No. If you want to get rid of the comma operator, this would be:

	ent = master;
	for (count = 0; ent; count++)
	{
  		ent = ent->chain;
  	}

But using the comma in the above situation is relatively common and 
there is no hazard that I know of doing so. Just know that the 
expressions will be evaluated in the order they appear (in case it 
matters, such as if they depend on one another, which is not the case in 
your example.)

[toc] | [prev] | [next] | [standalone]


#150741

FromGeoff <geoff@invalid.invalid>
Date2020-03-22 12:28 -0700
Message-ID<g3ff7ft3s56ovrueii9a7pg1q5k088d46a@4ax.com>
In reply to#150732
On Sun, 22 Mar 2020 17:31:12 +0100, Guillaume <message@bottle.org>
wrote:

>There is no comma operator here in the "tested expression".

I never said there was.

[toc] | [prev] | [next] | [standalone]


#150742

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2020-03-22 15:35 -0400
Message-ID<r58ela$dmh$1@dont-email.me>
In reply to#150741
On 3/22/20 3:28 PM, Geoff wrote:
> On Sun, 22 Mar 2020 17:31:12 +0100, Guillaume <message@bottle.org>
> wrote:
> 
>> There is no comma operator here in the "tested expression".
> 
> I never said there was.

You're right, and he's also right, but the reason why he mentioned that
is that your comment: "I'm aware of the hazards of using the comma
operator in the tested expression ..." would seem to be relevant only if
you thought there was a comma expression in the tested expression. If
you were aware that there was none, why did you bring that issue up?

[toc] | [prev] | [next] | [standalone]


#150745

FromGeoff <geoff@invalid.invalid>
Date2020-03-22 13:43 -0700
Message-ID<a9jf7fpmuq1dk11ct8vnkt3vmjht8j9b0l@4ax.com>
In reply to#150742
On Sun, 22 Mar 2020 15:35:06 -0400, James Kuyper
<jameskuyper@alumni.caltech.edu> wrote:

>On 3/22/20 3:28 PM, Geoff wrote:
>> On Sun, 22 Mar 2020 17:31:12 +0100, Guillaume <message@bottle.org>
>> wrote:
>> 
>>> There is no comma operator here in the "tested expression".
>> 
>> I never said there was.
>
>You're right, and he's also right, but the reason why he mentioned that
>is that your comment: "I'm aware of the hazards of using the comma
>operator in the tested expression ..." would seem to be relevant only if
>you thought there was a comma expression in the tested expression. If
>you were aware that there was none, why did you bring that issue up?

As I stated in my reply to Ben Bacarisse and indirectly to you, I said
I regretted that I had. It was part of my research on the topic of
comma operators in for-loops but I was not germane to the problem. My
original purpose in bringing it up was to preempt any discussion of
that separate problem. I was not successful.

[toc] | [prev] | [next] | [standalone]


#150738

FromLes Cargill <lcargill99@comcast.com>
Date2020-03-22 13:50 -0500
Message-ID<r58bsb$nhk$2@dont-email.me>
In reply to#150711
Geoff wrote:
> I'm aware of the hazards of using the comma operator in the tested
> expression but I ran across this example and I think it should be
> refactored, but how?
> 
> 	for (count = 0, ent = master; ent; ent = ent->chain, count++)
> 				;
> 
> 
> My first instinct is to write it:
> 
> 		for (count = 0; ent; count++)
> 		{
> 			ent = master;
> 			ent = ent->chain;
> 		}
> 
> But is this correct?
> 


I'd do the second just to avoid causing this question in others.

Dumber is better.

--
Les Cargill

[toc] | [prev] | [next] | [standalone]


#150740

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2020-03-22 19:25 +0000
Message-ID<87imiw5gzv.fsf@bsb.me.uk>
In reply to#150738
Les Cargill <lcargill99@comcast.com> writes:

> Geoff wrote:
>> I'm aware of the hazards of using the comma operator in the tested
>> expression but I ran across this example and I think it should be
>> refactored, but how?
>>
>> 	for (count = 0, ent = master; ent; ent = ent->chain, count++)
>> 				;
>>
>>
>> My first instinct is to write it:
>>
>> 		for (count = 0; ent; count++)
>> 		{
>> 			ent = master;
>> 			ent = ent->chain;
>> 		}
>>
>> But is this correct?
>
>
> I'd do the second just to avoid causing this question in others.
>
> Dumber is better.

I was expecting a smilie, but maybe you are serious.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#150746

FromDavid Brown <david.brown@hesbynett.no>
Date2020-03-22 21:52 +0100
Message-ID<r58j67$ich$1@dont-email.me>
In reply to#150740
On 22/03/2020 20:25, Ben Bacarisse wrote:
> Les Cargill <lcargill99@comcast.com> writes:
> 
>> Geoff wrote:
>>> I'm aware of the hazards of using the comma operator in the tested
>>> expression but I ran across this example and I think it should be
>>> refactored, but how?
>>>
>>> 	for (count = 0, ent = master; ent; ent = ent->chain, count++)
>>> 				;
>>>
>>>
>>> My first instinct is to write it:
>>>
>>> 		for (count = 0; ent; count++)
>>> 		{
>>> 			ent = master;
>>> 			ent = ent->chain;
>>> 		}
>>>
>>> But is this correct?
>>
>>
>> I'd do the second just to avoid causing this question in others.
>>
>> Dumber is better.
> 
> I was expecting a smilie, but maybe you are serious.
> 

I would not say "dumber is better", but I /would/ say "simple is better, 
all else being equal".  I'd avoid the commas and re-factor because I 
think it is simpler and clearer if the "for" part handles an obvious 
iterator ("ent" in this case) than a complex multi-part iterator.  But 
that is /simpler/, not /dumber/.

[toc] | [prev] | [next] | [standalone]


#150748

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2020-03-22 21:15 +0000
Message-ID<87zhc83xbl.fsf@bsb.me.uk>
In reply to#150746
David Brown <david.brown@hesbynett.no> writes:

> On 22/03/2020 20:25, Ben Bacarisse wrote:
>> Les Cargill <lcargill99@comcast.com> writes:
>>
>>> Geoff wrote:
>>>> I'm aware of the hazards of using the comma operator in the tested
>>>> expression but I ran across this example and I think it should be
>>>> refactored, but how?
>>>>
>>>> 	for (count = 0, ent = master; ent; ent = ent->chain, count++)
>>>> 				;
>>>>
>>>>
>>>> My first instinct is to write it:
>>>>
>>>> 		for (count = 0; ent; count++)
>>>> 		{
>>>> 			ent = master;
>>>> 			ent = ent->chain;
>>>> 		}
>>>>
>>>> But is this correct?
>>>
>>>
>>> I'd do the second just to avoid causing this question in others.
>>>
>>> Dumber is better.
>>
>> I was expecting a smilie, but maybe you are serious.
>>
>
> I would not say "dumber is better", but I /would/ say "simple is
> better, all else being equal".  I'd avoid the commas and re-factor
> because I think it is simpler and clearer if the "for" part handles an
> obvious iterator ("ent" in this case) than a complex multi-part
> iterator.  But that is /simpler/, not /dumber/.

Yes, I got the intended meaning from "dumber".  My point was "doing the
second" is wrong.  What's more, since it's the simpler/clearer/less
tricksy version, surely the fact that it's wrong should stand out a
mile?

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#150756

FromDavid Brown <david.brown@hesbynett.no>
Date2020-03-23 09:17 +0100
Message-ID<r59rbo$p1b$1@dont-email.me>
In reply to#150748
On 22/03/2020 22:15, Ben Bacarisse wrote:
> David Brown <david.brown@hesbynett.no> writes:
> 
>> On 22/03/2020 20:25, Ben Bacarisse wrote:
>>> Les Cargill <lcargill99@comcast.com> writes:
>>>
>>>> Geoff wrote:
>>>>> I'm aware of the hazards of using the comma operator in the tested
>>>>> expression but I ran across this example and I think it should be
>>>>> refactored, but how?
>>>>>
>>>>> 	for (count = 0, ent = master; ent; ent = ent->chain, count++)
>>>>> 				;
>>>>>
>>>>>
>>>>> My first instinct is to write it:
>>>>>
>>>>> 		for (count = 0; ent; count++)
>>>>> 		{
>>>>> 			ent = master;
>>>>> 			ent = ent->chain;
>>>>> 		}
>>>>>
>>>>> But is this correct?
>>>>
>>>>
>>>> I'd do the second just to avoid causing this question in others.
>>>>
>>>> Dumber is better.
>>>
>>> I was expecting a smilie, but maybe you are serious.
>>>
>>
>> I would not say "dumber is better", but I /would/ say "simple is
>> better, all else being equal".  I'd avoid the commas and re-factor
>> because I think it is simpler and clearer if the "for" part handles an
>> obvious iterator ("ent" in this case) than a complex multi-part
>> iterator.  But that is /simpler/, not /dumber/.
> 
> Yes, I got the intended meaning from "dumber".  My point was "doing the
> second" is wrong.  What's more, since it's the simpler/clearer/less
> tricksy version, surely the fact that it's wrong should stand out a
> mile?
> 

I was answering more as a general point - simpler is better than complex 
when there is a choice - rather than looking at the specific case here. 
I assumed Les was thinking in the same lines.  But his choice of 
"dumber" was bad - it /can/ mean "simpler", but it can also mean "stupider".

Maybe I got the wrong end of the stick here, and he was being sarcastic.

(Yes, with simpler code mistakes like the one above are usually easier 
to spot - and usually even more easy to avoid making in the first place.)

[toc] | [prev] | [next] | [standalone]


#150861

FromTim Rentsch <tr.17687@z991.linuxsc.com>
Date2020-03-27 08:02 -0700
Message-ID<86mu8196yl.fsf@linuxsc.com>
In reply to#150711
Geoff <geoff@invalid.invalid> writes:

> I'm aware of the hazards of using the comma operator in the tested
> expression but I ran across this example and I think it should be
> refactored, but how?
>
>     for (count = 0, ent = master; ent;  ent = ent->chain, count++)
>                 ;

I will offer a different view here.  There is nothing wrong with
using the comma operator in for-expressions.  Indeed it is uses
in for-expressions that provides what may be the clearest case
in favor of using the comma operator.

The problem here, if there is a problem, is the "count++" in
the third for-expression.  Logically the 'count++' operation
seems like a better fit for the loop body:

    for (count = 0, ent = master; ent;  ent = ent->chain)  count++;

I'm sure some people would prefer the 'count = 0' to be put as a
separate statement, before the for() loop.  Personally I think it
makes more sense to include it in the first for-expression, because
it is part of establishing the invariant for the loop.  Probably
this boils down to how one conceptualizes C's for() statement.
Some people think of for() in C as a slightly more general form
of a FORTRAN DO-loop.  My view, which I think also reflects the
views of C's original authors, is for() in C offers a generalized
looping structure, with loop initialization, loop prelude, and
loop advancement sections.  That perspective suggests it is
better to put 'count = 0' in the first for-expression than as a
separate statement before the for() statement.  I realize other
people sometimes view it differently, and I think I understand
their reasons for the preference they have.  I just don't think
either view is "right" is some absolute sense, only a reflection
of how one conceptualizes C's for() construct.

[toc] | [prev] | [standalone]


Page 6 of 6 — ← Prev page 1 2 3 4 5 [6]

Back to top | Article view | comp.lang.c


csiph-web