3 ms·
See I always thought most people who wrote foo++ actually meant ++foo and it typically only came up when the operator precedence made the two not equivalent, ca
by kinofcain 10y ago
See I always thought most people who wrote foo++ actually meant ++foo and it typically only came up when the operator precedence made the two not equivalent, causing a subtle bug.
I can't even think of a time where I really needed a use-then-increment operator where I wouldn't have just used the value, then incremented it on the next line anyway (maybe even with a post-increment out of habit).
- mmastrac 10y agoInteresting. I generally choose x++ for the majority of array-striding code I write, and usually as a shortcut to avoid having a bunch of x++ lines scattered around. I've found that you can structure your code either way and postfix seems to be easier to reason over for me. I use ++x very infrequently, and mainly in hand-written text parsers.
- sbmassey 10y agoI generally assume that ++x is idiomatic in c++, and x++ in c, because in c++ you may be calling against some non-primitive type, where the update-and-return-the-non-updated-value semantics of the postfix version are not optimisable away.
- kinofcain 10y agoWell, I guess we're both going to do it the same way in swift 3, which is probably the whole point...
- stormbrew 10y agoint arr[5]; int i = 0; while (shouldIKeepGoing() && i < 5) arr[i++] = getAThing(); If you use ++i there you'll skip 0 and overflow the array. This is what the postfix form is for, if you value terseness. Edit: To clarify, I'm not necessarily saying you don't know that's what it's for, but this is a pattern I see pretty frequently in the wild so I'm just throwing it out there with an explanation of why people find value in it.