POLL: Programming style - i++ vs. ++i
-
After a batch of interviews, the second most notable thing is that where both
++i
andi++
are possible, all candidates usedi++
Having optimizied for older compilers I automatically opt for the "simpler" concept of ++i except where I explicitely need the postfix increment. (I now that it makes no difference to today's compilers, but it might still save some microseconds when compiling :rolleyes: ) so - Vote 5 if it'sfor(int i=0; i<n; ++i)
, and Vote 1 if it'sfor(int i=0; i<n; i++)
for you. if the "programming" in the title triggers a pawlowian in you, please vote 3
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygenI wish there was a neither option. I try to avoid both and use i += 1. Two reasons : I wrote a scripting engine and I saw little reason for the ++ and -- operators and did not include them. This got me into the habit of using the += and -= operators which I did include. Second reason - if, for some odd reason, the incrementer needs to change to a 2 it is easier. Actually, a macro or "const int" value for the incrementer is a better way to go which I prefer to use along with the += and -= operators. Bottom line - I prefer the += method to be as consistent as possible but that's just me. __________________________________________ a two cent stamp short of going postal.
-
After a batch of interviews, the second most notable thing is that where both
++i
andi++
are possible, all candidates usedi++
Having optimizied for older compilers I automatically opt for the "simpler" concept of ++i except where I explicitely need the postfix increment. (I now that it makes no difference to today's compilers, but it might still save some microseconds when compiling :rolleyes: ) so - Vote 5 if it'sfor(int i=0; i<n; ++i)
, and Vote 1 if it'sfor(int i=0; i<n; i++)
for you. if the "programming" in the title triggers a pawlowian in you, please vote 3
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygen++i definitely. I'd use != instead of < too, seems so wasteful otherwise :) Ryan
-
After a batch of interviews, the second most notable thing is that where both
++i
andi++
are possible, all candidates usedi++
Having optimizied for older compilers I automatically opt for the "simpler" concept of ++i except where I explicitely need the postfix increment. (I now that it makes no difference to today's compilers, but it might still save some microseconds when compiling :rolleyes: ) so - Vote 5 if it'sfor(int i=0; i<n; ++i)
, and Vote 1 if it'sfor(int i=0; i<n; i++)
for you. if the "programming" in the title triggers a pawlowian in you, please vote 3
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygenIf I remember the theory correctly, the pre-increment operator is more efficient than the post-increment operator. The argument states that the post-increment operator, to be completely correct, is implemented by copy-contructing a temporary with the current value, followed by performing the increment on the original value. The pre-increment operator doesn't require the copy-constructed temporary, since the expression states you want to increment the value and then use it. I've always thought the argument was specious. In most cases where you are using an increment operator, the value being operated upon is a 'simple' value of some form, where the increment operation is a natural, intuitive operation for the value. I'm sure the compiler can optimize any inefficiencies in these cases such that the performance difference between pre- and post- increment operations is negligible. Personally, the remaining cases represent poor design as far as I'm concerned. Anytime I've seen code that implemented increment operators on a complex class, it's been programmer ego at work. "Look at this big honking operation I can do with this class, and just in an operator!" In answer to your question, I always use the post-increment operator. It's a heritage left over from learning C from Kernighan & Ritchie, which almost invariably coded things as
for (i = 0; i < n; i++)
.
Software Zen:
delete this;
-
If I remember the theory correctly, the pre-increment operator is more efficient than the post-increment operator. The argument states that the post-increment operator, to be completely correct, is implemented by copy-contructing a temporary with the current value, followed by performing the increment on the original value. The pre-increment operator doesn't require the copy-constructed temporary, since the expression states you want to increment the value and then use it. I've always thought the argument was specious. In most cases where you are using an increment operator, the value being operated upon is a 'simple' value of some form, where the increment operation is a natural, intuitive operation for the value. I'm sure the compiler can optimize any inefficiencies in these cases such that the performance difference between pre- and post- increment operations is negligible. Personally, the remaining cases represent poor design as far as I'm concerned. Anytime I've seen code that implemented increment operators on a complex class, it's been programmer ego at work. "Look at this big honking operation I can do with this class, and just in an operator!" In answer to your question, I always use the post-increment operator. It's a heritage left over from learning C from Kernighan & Ritchie, which almost invariably coded things as
for (i = 0; i < n; i++)
.
Software Zen:
delete this;
Gary R. Wheeler wrote: Anytime I've seen code that implemented increment operators on a complex class, it's been programmer ego at work. "Look at this big honking operation I can do with this class, and just in an operator!" Agreed 100% gary. Gary R. Wheeler wrote: I always use the post-increment operator Me too. I started doing it that way more than a decade ago, and it's habit now. If I do
++i
instead ofi++
in afor
loop, it won't even feel as if it's my code. :rolleyes: Nish -
If I remember the theory correctly, the pre-increment operator is more efficient than the post-increment operator. The argument states that the post-increment operator, to be completely correct, is implemented by copy-contructing a temporary with the current value, followed by performing the increment on the original value. The pre-increment operator doesn't require the copy-constructed temporary, since the expression states you want to increment the value and then use it. I've always thought the argument was specious. In most cases where you are using an increment operator, the value being operated upon is a 'simple' value of some form, where the increment operation is a natural, intuitive operation for the value. I'm sure the compiler can optimize any inefficiencies in these cases such that the performance difference between pre- and post- increment operations is negligible. Personally, the remaining cases represent poor design as far as I'm concerned. Anytime I've seen code that implemented increment operators on a complex class, it's been programmer ego at work. "Look at this big honking operation I can do with this class, and just in an operator!" In answer to your question, I always use the post-increment operator. It's a heritage left over from learning C from Kernighan & Ritchie, which almost invariably coded things as
for (i = 0; i < n; i++)
.
Software Zen:
delete this;
Gary R. Wheeler wrote: If I remember the theory correctly, the pre-increment operator is more efficient.. completely correct (or at least, you remember the same as I do :cool: ) Some years ago, the compilers I worked with did often fail to optimize the temporary away with the post-increment. Things have gotten better, though...
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygen -
Gary R. Wheeler wrote: If I remember the theory correctly, the pre-increment operator is more efficient.. completely correct (or at least, you remember the same as I do :cool: ) Some years ago, the compilers I worked with did often fail to optimize the temporary away with the post-increment. Things have gotten better, though...
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygenIn terms of compiler optimization intelligence, I think we've finally surpassed the smarts of the VAX FORTRAN compiler of the late 80's. A human being could not write code that was more efficient than what that compiler generated.
Software Zen:
delete this;
-
After a batch of interviews, the second most notable thing is that where both
++i
andi++
are possible, all candidates usedi++
Having optimizied for older compilers I automatically opt for the "simpler" concept of ++i except where I explicitely need the postfix increment. (I now that it makes no difference to today's compilers, but it might still save some microseconds when compiling :rolleyes: ) so - Vote 5 if it'sfor(int i=0; i<n; ++i)
, and Vote 1 if it'sfor(int i=0; i<n; i++)
for you. if the "programming" in the title triggers a pawlowian in you, please vote 3
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygenI prefer i++ instead of ++i, for a totally different reason. Consider two weight lifters, they are both starved to qualify for the 55kg competition. The i++ guy can eat 1 kg of delicious food and still qualify for the 55kg class, while the ++i guy has to wait until after the match to eat anything. P.S. May be this is not a good joke. :)[
My articles and software tools
-
After a batch of interviews, the second most notable thing is that where both
++i
andi++
are possible, all candidates usedi++
Having optimizied for older compilers I automatically opt for the "simpler" concept of ++i except where I explicitely need the postfix increment. (I now that it makes no difference to today's compilers, but it might still save some microseconds when compiling :rolleyes: ) so - Vote 5 if it'sfor(int i=0; i<n; ++i)
, and Vote 1 if it'sfor(int i=0; i<n; i++)
for you. if the "programming" in the title triggers a pawlowian in you, please vote 3
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygenI absolutely detest
++i
, but for no good reason... it just looks weird. :omg: --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- You cannot truly appreciate Dilbert unless you've read it in the original Klingon. -
After a batch of interviews, the second most notable thing is that where both
++i
andi++
are possible, all candidates usedi++
Having optimizied for older compilers I automatically opt for the "simpler" concept of ++i except where I explicitely need the postfix increment. (I now that it makes no difference to today's compilers, but it might still save some microseconds when compiling :rolleyes: ) so - Vote 5 if it'sfor(int i=0; i<n; ++i)
, and Vote 1 if it'sfor(int i=0; i<n; i++)
for you. if the "programming" in the title triggers a pawlowian in you, please vote 3
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygen++i, for sure. Although a compiler may optimise, it's in theory more efficient, and never less so. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
-
After a batch of interviews, the second most notable thing is that where both
++i
andi++
are possible, all candidates usedi++
Having optimizied for older compilers I automatically opt for the "simpler" concept of ++i except where I explicitely need the postfix increment. (I now that it makes no difference to today's compilers, but it might still save some microseconds when compiling :rolleyes: ) so - Vote 5 if it'sfor(int i=0; i<n; ++i)
, and Vote 1 if it'sfor(int i=0; i<n; i++)
for you. if the "programming" in the title triggers a pawlowian in you, please vote 3
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygenI rarely use ++i because then I would have to take a few seconds to think on which order things are executed... John
-
In terms of compiler optimization intelligence, I think we've finally surpassed the smarts of the VAX FORTRAN compiler of the late 80's. A human being could not write code that was more efficient than what that compiler generated.
Software Zen:
delete this;
-
I rarely use ++i because then I would have to take a few seconds to think on which order things are executed... John
I use
foreach
. :) Jonathan de Halleux - My Blog -
I wish there was a neither option. I try to avoid both and use i += 1. Two reasons : I wrote a scripting engine and I saw little reason for the ++ and -- operators and did not include them. This got me into the habit of using the += and -= operators which I did include. Second reason - if, for some odd reason, the incrementer needs to change to a 2 it is easier. Actually, a macro or "const int" value for the incrementer is a better way to go which I prefer to use along with the += and -= operators. Bottom line - I prefer the += method to be as consistent as possible but that's just me. __________________________________________ a two cent stamp short of going postal.
Rick York wrote: I saw little reason for the ++ and -- operators and did not include them You code in C
**++**
and do not see the need for the ++ operator? :omg::wtf: Yes, even I am blogging now! -
After a batch of interviews, the second most notable thing is that where both
++i
andi++
are possible, all candidates usedi++
Having optimizied for older compilers I automatically opt for the "simpler" concept of ++i except where I explicitely need the postfix increment. (I now that it makes no difference to today's compilers, but it might still save some microseconds when compiling :rolleyes: ) so - Vote 5 if it'sfor(int i=0; i<n; ++i)
, and Vote 1 if it'sfor(int i=0; i<n; i++)
for you. if the "programming" in the title triggers a pawlowian in you, please vote 3
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygen++i is safe there but a bad habit in other places so I stick with i++. Elaine :rose: The tigress is here :-D
-
I use
foreach
. :) Jonathan de Halleux - My Blog -
++i is safe there but a bad habit in other places so I stick with i++. Elaine :rose: The tigress is here :-D
-
++i, for sure. Although a compiler may optimise, it's in theory more efficient, and never less so. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
-
After a batch of interviews, the second most notable thing is that where both
++i
andi++
are possible, all candidates usedi++
Having optimizied for older compilers I automatically opt for the "simpler" concept of ++i except where I explicitely need the postfix increment. (I now that it makes no difference to today's compilers, but it might still save some microseconds when compiling :rolleyes: ) so - Vote 5 if it'sfor(int i=0; i<n; ++i)
, and Vote 1 if it'sfor(int i=0; i<n; i++)
for you. if the "programming" in the title triggers a pawlowian in you, please vote 3
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygen -
After a batch of interviews, the second most notable thing is that where both
++i
andi++
are possible, all candidates usedi++
Having optimizied for older compilers I automatically opt for the "simpler" concept of ++i except where I explicitely need the postfix increment. (I now that it makes no difference to today's compilers, but it might still save some microseconds when compiling :rolleyes: ) so - Vote 5 if it'sfor(int i=0; i<n; ++i)
, and Vote 1 if it'sfor(int i=0; i<n; i++)
for you. if the "programming" in the title triggers a pawlowian in you, please vote 3
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygenpeterchen wrote: please vote 3 Won't that skew the results? I thought different levels of members got different vote weights. :~ FWIW I'm an i++ guy.
David Wulff The Royal Woofle Museum
Putting the laughter back into slaughter
-
After a batch of interviews, the second most notable thing is that where both
++i
andi++
are possible, all candidates usedi++
Having optimizied for older compilers I automatically opt for the "simpler" concept of ++i except where I explicitely need the postfix increment. (I now that it makes no difference to today's compilers, but it might still save some microseconds when compiling :rolleyes: ) so - Vote 5 if it'sfor(int i=0; i<n; ++i)
, and Vote 1 if it'sfor(int i=0; i<n; i++)
for you. if the "programming" in the title triggers a pawlowian in you, please vote 3
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygenStrange for incrementing integers in for statements I use i++ For incrementing itterators I use ++it. Ant. I'm hard, yet soft.
I'm coloured, yet clear.
I'm fuity and sweet.
I'm jelly, what am I? Muse on it further, I shall return! - David Williams (Little Britain)