Timeline for Why are shortcuts like x += y considered good practice?
Current License: CC BY-SA 3.0
9 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| May 19, 2015 at 22:55 | comment | added | John Hascall | None of the expressions ++x + 3 , x += 1 + 3 or (x += 1) + 3 have undefined behavior (assuming the resulting value "fits"). | |
| May 19, 2015 at 21:36 | comment | added | Lightness Races in Orbit | @DavidThornley: It's hard to say "they're identical" when they both have undefined behaviour :) | |
| Feb 10, 2012 at 21:21 | comment | added | David Thornley | @Donal Fellows: The precedence is different, so ++x + 3 isn't the same as x += 1 + 3. Parenthesize the x += 1 and it's identical. As statements by themselves, they're identical. | |
| Feb 10, 2012 at 18:52 | comment | added | Gary Willoughby | My comment was to highlight that Ilmari's comment might not be accurate. | |
| Feb 10, 2012 at 10:02 | comment | added | Donal Fellows | @Gary: ++x and x+=1 are equivalent in C and Java (maybe also C#) though not necessarily so in C++ because of the complex operator semantics there. The key is that they both evaluate x once, increment the variable by one, and have a result that is the content of the variable after evaluation. | |
| Feb 9, 2012 at 14:28 | history | made wiki | Post Made Community Wiki by ctrl-alt-delor | ||
| Feb 9, 2012 at 13:44 | comment | added | Gary Willoughby | x++ and ++x are slightly different to x+=1 and to each other. | |
| Feb 9, 2012 at 13:17 | comment | added | Ilmari Karonen | ...which is why, in C, there's an even more specific shorthand notation for that: ++x (and/or x++). | |
| Feb 9, 2012 at 5:57 | history | answered | Joe | CC BY-SA 3.0 |