Friday, September 28, 2007

What does ++i mean?

What does "++i" mean? Well, it means "add 1 to i and return the value of i". This differs from "i++", which means "add 1 and return the value of i-1" or "return the value of i, then add 1 to it". Honestly, I'd prefer that such mechanisms weren't used as components of evaluated expressions. They are similar syntactically, but they differ in their results. These results are different enough to cause all sort of problems if not understood.

Performance-wise, historically "++i" has been favored. I'm not sure exactly why it had better performance. But I'm convinced that this performance difference is unobservable in the light of current CPU and compiler technology.

I must admit that the behavior of the "i++" operation is more confusing. Unlike most expressions, it returns the value of the variable before the operation occurs, rather than after. This is a break in coherence with the "+" operator. Or any operator, for that matter.

So why am I supporting the postfix "i++" operator over the prefix operator? Not because its returned value is more sensible. I think I've established that using these operators in evaluated expressions is a no-no. Rather, I prefer "i++" because it more closely resembles its mathematical analogue: "i = i + 1". What does "++i" resemble?: "1 + i = i". That lacks any coherence with mathematics- it suggests an operation opposite of its actual result. In the end "i++" trades coherence within the programming language for coherence with the language of mathematics. And "++i" does the opposite. And I prefer mathematics.

7 comments:

Bossk said...

Analog Schmamalog. And note the PROPER spelling, not some Frenchified version used by whipped dogs. One does not compare mathematical notations because one looks like another. The equation "i = i + 1" (or "i + 1 = i") has the empty set as its solution; "++" is technically a *function* on the set of integers. Although both prefix and postfix notation are common in modern algebra, the postfix version in C programming cannot be a operator in any mathematical meaning of the word--its value is different from the value obtained by applying the operation!

schwerwolf said...

I wasn't suggesting that the mathematical analogue should be overextended, as you have done. I was suggesting that some coherence with mathematics is preferable to the backwards prefix notation.

Your statements have helped me determine that a stronger coherence lies not with mathematics, but with left-to-right languages. The reason "i++" is superior to "++i" is the analogous to the reason why "i=i+1" is syntactically correct and "i+1=i" is not.

Perhaps if you were Hebrew or Arabic (no offense intended) prefix would be the preferred notation.

Bossk said...

Your analog is off the mark. To use a comparison to natural language, "i" is an object and "++" is a verb (increment). Both "++i;" and "i++;" are equivalent to imperative sentences: "Increment the loop variable!" and "The loop variable increment!" Obviously one is grammatical and the other is not.

schwerwolf said...

That's a nice piece of translation you've done- but again it's inaccurate. We don't translate "don't => don + 't". Rather, we expand the abbreviation, and then translate. The expansion for "i++" is obvious: "i = i + 1". The expansion for "++i" is counterintuitive. I think I'm repeating myself.

Bossk said...

I don't know how much you're repeating yourself, but whatever it is you're smoking, it must sure be good!

RibRdb said...

I don't think compilers can optimize i++ to ++i when you're using C++ objects with operator overloading. i++ has to make a copy, even if this copy isn't used because copying can have side effects.

schwerwolf said...

I'm personally against overloading the operator++ (or any operator for that matter) unless there is a very strong mathematical metaphor. I'm not even a big fan of iterator++. But your point is taken.