2

Possible Duplicate:
Difference between i++ and ++i in a loop?
Is there a performance difference between i++ and ++i in C++?
Incrementing in C++ - When to use x++ or ++x?
Why use ++i instead of i++ in cases where the value is not used anywhere else in the statement?

Why in C++ textbooks is there a preference for writing ++x rather than x++ when this occurs in a context where the pre/post nature doesn't matter ?

In general, it seems that actions are given in object,verb order

eg:

foo.size() is the 'object' foo , with 'verb' size

a + b is 'object' a , with verb +

In EXCEL you always select the object , then specify the action (verb).

note : Lotus 1-2-3 did things in verb-object order which caused enormous problems for people who had developed muscle memory in the 123 to XL transition...

Community
  • 1
  • 1
user1202733
  • 623
  • 4
  • 10
  • 1
    possible duplicate of [Difference between i++ and ++i in a loop?](http://stackoverflow.com/questions/484462/difference-between-i-and-i-in-a-loop) and especially [this answer](http://stackoverflow.com/a/484492/103167) Also http://stackoverflow.com/questions/24901/is-there-a-performance-difference-between-i-and-i-in-c – Ben Voigt Feb 19 '12 at 01:17
  • I wouldn't call it a duplicate since he is asking in general since and not in just loops. – Yamiko Feb 19 '12 at 01:18
  • You should really read a beginner's guide to C++. Analogies to Excel won't help you much. – Don Reba Feb 19 '12 at 01:18
  • 4
    Because if your compiler was really stupid, `i++` would needlessly store a temporary copy of `i`. It isn't, so it doesn't matter, but that's why `++i` was accepted as the best practice. – James M Feb 19 '12 at 01:19
  • @yamikoWebs: How about [this question](http://stackoverflow.com/questions/24901/is-there-a-performance-difference-between-i-and-i-in-c) then? – Ben Voigt Feb 19 '12 at 01:19
  • 1
    They do it for consistency: it matters a lot with iterators, so they keep using `++x` everywhere else for the code samples in their books to look consistent. – Sergey Kalinichenko Feb 19 '12 at 01:20
  • No -- this recognises the difference. But asks why there is an apparent preference when the difference doesn't matter. – user1202733 Feb 19 '12 at 01:20
  • @JamesMcLaughlin I never read that before in any of my c++ textbooks. Thanks for the info – Yamiko Feb 19 '12 at 01:24

2 Answers2

4

I prefer ++x over x++ because, to me, it emphasizes the increment operation over the name of the variable. It's strictly a matter of preference, but I think it highlights my intention more clearly.

More important is that you choose one or the other and use it consistently. Code that's peppered with ++x and x++ used arbitrarily when their effect is identical is just a recipe for unmaintainability. Sooner or later, someone's going to "fix" it to be consistent, and they'll probably introduce bugs when they change an instance that really does matter.

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
3

i++ will create a copy of the object i. If i is a complex iterator, this may reduce performance considerably, compared to ++i.

rasmus
  • 3,136
  • 17
  • 22