Search code examples
c++post-incrementpre-increment

Why do textbooks prefer "++x" to "x++" when it is context invariant?


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...


Solution

  • 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.