Source-Changes-D archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: The --> "operator"
On Wed, Jul 08, 2026 at 07:44:42AM -0400, Greg Troxel wrote:
> Martin Husemann <martin%duskware.de@localhost> writes:
>
> > On Wed, Jul 08, 2026 at 06:56:01AM -0400, Greg Troxel wrote:
> >> I have never seen --> in C, from reading K&R in 1977 to now. It looks
> >> like something made up by C++ :-) But seriously, I find it unhelpful.
> >> The semantics of --i and i-- are clear and have been stable for years,
> >> as are the semantics of > and >=.
> >
> > There is nothing special about it, and it doesn't matter if you
> > write it as -- > or --> and give the latter a cute name.
> >
> > It is two operators, post-decrement and greater-than and it has been
> > there from the beginning.
>
> I meant that writing it as "-->" is irregular, and I find it unhelpful.
>
> I also find i-- in a for loop update step (middle) to be unhelpful for
> clarity, as the test is done on a different value that the loop sees.
>
> This feels like "it's twice as hard to debug code than to write it, so
> only write code that is half as clever as you are capable of". I found
> that "writing for the code reviewer" led to code that was as simple and
> clear as it could be, and this helped reliability, back when I was
> in an environment with review.
As someone who has actually tried to write, from scratch, reverse
iteration from ${UNSIGNEDTYPE}_MAX down to and including zero loops in C
correctly, using the same unsigned type as the index, I have to say that
it is not as obvious as you might think.
I kept trying to make it look like forward iteration with the index
adjustment in the third for() position, but there is no way to make it
work correctly in all implementations that way.
I tried to do index adjustment within the loop body itself. I quickly
realized that I did not want to rely on the implementation-defined
underflow/overflow behavior you need to be defined when you alter the
index into another index variable within the loop body.
With Taylor's suggestion of post-decrement in the condition position, I
got it working.
I can't say I have a strong preference for 'i --> 0' over 'i-- > 0', but
having it be contrary to typical 'style' certainly makes the atypical
idiom stand out. It probably needs to stand out; but it doesn't need to
stand out as much as a whole block comment before such a loop.
Jonathan Kollasch
Home |
Main Index |
Thread Index |
Old Index