logoalt Hacker News

kenferry • today at 1:31 AM • 9 replies • view on HN

I know it's "too clever", but if I'm writing code no one else will ever see,

  while (i --> 0) {
      // use array[i]
  }
easy to read (if not understand), and correct!

Replies

krackers • today at 1:38 AM

Oh I remember this from the classic SO post, but it was just presented as a funny trick. I never realized that it solves the issue mentioned in the article and there is a legitimate use-case for post-decrement in the comparison itself.

https://stackoverflow.com/questions/1642028/what-is-the-oper...

➕ show 2 replies
cryptonector • today at 2:52 AM

It's a well-known (ish) idiom that nobody uses. It's very entertaining though, for sure. In some circles it's known as the down-to operator.

viega • today at 1:46 AM

I always use unsigned values and do:

  while (i-- != 0) { ... }
It's more clear than the most idiomatic way, which is to drop the explicit comparison:

  while (i--) { ... }
 
All of it is better than the clunky old for-loop syntax.
➕ show 1 reply
Razengan • today at 5:48 AM

This is why we never escape the cycle of rebirth.

golden-face • today at 3:32 AM

Praise to the old gods and the new, why?!

mrheosuper • today at 4:03 AM

Code like this is why i prefer human code more than llm.

Reading code should be enjoyable.

mtklein • today at 2:22 AM

I don't think it's too clever at all. Notably it works with both signed and unsigned i. Frankly if I see anyone doing anything other than this, they're wrong.

stephencanon • today at 1:33 AM

Always.

(Hi Ken!)

hujun236 • today at 2:51 AM

[dead]