Post increment and decrement

Ali Çehreli via Digitalmars-d digitalmars-d at puremagic.com
Wed Mar 11 11:10:22 PDT 2015


On 03/11/2015 10:23 AM, welkam wrote:

 > Observation Nr. 1
 > People prefer to write var++ instead of ++var.
 >
 > Observation Nr. 2
 > Because of observation Nr. 1 and other reasons compilers became good at
 > removing code that is not needed making var++ and ++var to produce the
 > same code if returned value is not used.
 >
 > Observation Nr. 3
 > Because of observation Nr. 2 more people use var++ in place where they
 > really only need ++var.
 >
 > Observation Nr. 4
 > Because of observation Nr. 3 people learning to program may mistakenly
 > learn that var++ is just incrementing. (I am included in that list)
 >
 > Observation Nr. 5
 > Because of observation Nr. 4 people can write slower than necessary code
 > for classes with overloaded operator or even get bugs.

+5 for all your observations. (Ok, 5 each, 25 total. :p)

I cringe every time I see var++ and var--. That's why I advise against them:

   http://ddili.org/ders/d.en/arithmetic.html#ix_arithmetic.post-increment

 > Because of all this why not make only one increment/decrement operator
 > and have post increment/decrement to be called by template name, because
 > it is a template?
 >
 > template post_inc(T) {
 > auto tmp = T;
 > T++;
 > return tmp;
 > }

Agreed but that train has already sailed (: having a good time today); 
there are too many programs out there doing var++.

Ali



More information about the Digitalmars-d mailing list