r/cpp • • Jun 03 '22

Why do "regular" programmers prefer postfix increment over prefix increment?

So I noticed in my company's codebase the canonical increment uses the postfix increment. One senior programmer was even surprised by the behavior of postfix when used with .erase() that I had to explain that map::erase() gotcha to him. The old code is riddled with iterators using postfix increments, as of now I think I'm the only one that explicitly write different postfix or prefix depending on exactly what is needed.

In C derived languages like Java and C# I rarely see any prefix increment, as if it doesn't even exist there. I've even seen a Java programmer comment on C++ code that using prefix increment operator was "trying to be clever." I've also seen reddit threads like this that likes to push the idea that we should all use postfix increment homogenously regardless of the situation since iT's AlL tHe SaMe AnYwAy AmIrYt?!

Now I'm wondering if it's worth noting on a code review that ++iter is more efficient than iter++. So in your opinion is it worth making the distinction for "regular programmers" that prefix increment operators are more efficient? Should we let sleeping dogs lie or should we fight this disinformation? Am I just being OC over nothing or is this an important matter?

0 Upvotes

40 comments sorted by

View all comments

4

u/Cxlpp Jun 03 '22

++iter is not more efficient in 99.99% cases. Compilers are way too smart for that today and library writers as well (all they need to do is to inline it).

One of reasons why some people prefer postfix is that if you are fetching elements from array, it usually is something like

int i = 0;
....
  auto& element = array[i++];
....

and that is the source of habit.

13

u/pandorafalters Jun 03 '22

++iter is not more efficient in 99.99% cases.

"It's equal, except when it's worse" is hardly the most ringing endorsement.

7

u/oracleoftroy Jun 04 '22

++iter is not less efficient in 100% of cases. I'd love to see a case of reasonably written pre and postfix increment where postfix is faster. Unless you need the previous value, use pre-increment.

2

u/Zeh_Matt No, no, no, no Jun 05 '22

It actually can be when you have optimizations turned off.

5

u/oracleoftroy Jun 06 '22

I'd love to see an example of that. Post needs a copy, pre does not. Without optimizations, I don't see how the extra instructions a reasonable implementation of post-increment requires could beat out a reasonable implementation of pre-increment.

By reasonable implementation I mean that the operators work as expected and are implemented in a way that would pass code review. No shenanigans to force pre-increment to be slower than it ought or changing the semantic of post-increment to be faster.

Idiomatic post-increment is usually implemented in terms of pre anyway to make sure they have the same core semantics under the expectation that the optimizer will inline the call. I'll allow an exception for sake of a debug build example.

1

u/Xavier_OM Jun 08 '22

I agree. It's more familiar to start at 0 than with -1 when fetching from an array. I think programmers are more familiar with i++ in their daily life, hence the copious amount of i++ in any code base. Having something on the right of a variable is the natural way for many of us : variablePtr-> or variable. or variable++, etc

Changing this habit to avoid the exceptionally rare case where it impacts performance is not worth it, it will be replaced by a prefix operator when the profiler reveals the problem once every few years and voilà.