Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. POLL: Programming style - i++ vs. ++i

POLL: Programming style - i++ vs. ++i

Scheduled Pinned Locked Moved The Lounge
htmlvisual-studiodesignbusinesshelp
26 Posts 19 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    peterchen
    wrote on last edited by
    #1

    After a batch of interviews, the second most notable thing is that where both ++i and i++ are possible, all candidates used i++ 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's for(int i=0; i<n; ++i) , and Vote 1 if it's for(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

    C R R G X 12 Replies Last reply
    0
    • P peterchen

      After a batch of interviews, the second most notable thing is that where both ++i and i++ are possible, all candidates used i++ 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's for(int i=0; i<n; ++i) , and Vote 1 if it's for(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

      C Offline
      C Offline
      ColinDavies
      wrote on last edited by
      #2

      Darn !! I voted wrong. -- I use both interchangeably. Which is probably really bad form. But I do think ++i is more correct. Regardz Colin J Davies

      *** WARNING *
      This could be addictive
      **The minion's version of "Catch :bob: "

      It's a real shame that people as stupid as you can work out how to use a computer. said by Christian Graus in the Soapbox

      1 Reply Last reply
      0
      • P peterchen

        After a batch of interviews, the second most notable thing is that where both ++i and i++ are possible, all candidates used i++ 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's for(int i=0; i<n; ++i) , and Vote 1 if it's for(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

        R Offline
        R Offline
        Rick York
        wrote on last edited by
        #3

        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.

        D 1 Reply Last reply
        0
        • P peterchen

          After a batch of interviews, the second most notable thing is that where both ++i and i++ are possible, all candidates used i++ 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's for(int i=0; i<n; ++i) , and Vote 1 if it's for(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

          R Offline
          R Offline
          Ryan Roberts
          wrote on last edited by
          #4

          ++i definitely. I'd use != instead of < too, seems so wasteful otherwise :) Ryan

          1 Reply Last reply
          0
          • P peterchen

            After a batch of interviews, the second most notable thing is that where both ++i and i++ are possible, all candidates used i++ 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's for(int i=0; i<n; ++i) , and Vote 1 if it's for(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

            G Offline
            G Offline
            Gary R Wheeler
            wrote on last edited by
            #5

            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;

            N P 2 Replies Last reply
            0
            • G Gary R Wheeler

              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;

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #6

              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 of i++ in a for loop, it won't even feel as if it's my code. :rolleyes: Nish

              1 Reply Last reply
              0
              • G Gary R Wheeler

                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;

                P Offline
                P Offline
                peterchen
                wrote on last edited by
                #7

                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

                G 1 Reply Last reply
                0
                • P peterchen

                  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

                  G Offline
                  G Offline
                  Gary R Wheeler
                  wrote on last edited by
                  #8

                  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;

                  RaviBeeR 1 Reply Last reply
                  0
                  • P peterchen

                    After a batch of interviews, the second most notable thing is that where both ++i and i++ are possible, all candidates used i++ 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's for(int i=0; i<n; ++i) , and Vote 1 if it's for(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

                    X Offline
                    X Offline
                    Xiangyang Liu
                    wrote on last edited by
                    #9

                    I 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

                    ](http://mysite.verizon.net/XiangYangL/index.htm)

                    1 Reply Last reply
                    0
                    • P peterchen

                      After a batch of interviews, the second most notable thing is that where both ++i and i++ are possible, all candidates used i++ 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's for(int i=0; i<n; ++i) , and Vote 1 if it's for(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

                      M Offline
                      M Offline
                      Michael Dunn
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • P peterchen

                        After a batch of interviews, the second most notable thing is that where both ++i and i++ are possible, all candidates used i++ 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's for(int i=0; i<n; ++i) , and Vote 1 if it's for(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

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #11

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

                        P 1 Reply Last reply
                        0
                        • P peterchen

                          After a batch of interviews, the second most notable thing is that where both ++i and i++ are possible, all candidates used i++ 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's for(int i=0; i<n; ++i) , and Vote 1 if it's for(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

                          J Offline
                          J Offline
                          John M Drescher
                          wrote on last edited by
                          #12

                          I rarely use ++i because then I would have to take a few seconds to think on which order things are executed... John

                          J 1 Reply Last reply
                          0
                          • G Gary R Wheeler

                            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;

                            RaviBeeR Offline
                            RaviBeeR Offline
                            RaviBee
                            wrote on last edited by
                            #13

                            Memories... (my Mass license plate is VAX-VMS) :cool: Spent 8 very happy years @ Digital (when it was still Digital). /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

                            G 1 Reply Last reply
                            0
                            • J John M Drescher

                              I rarely use ++i because then I would have to take a few seconds to think on which order things are executed... John

                              J Offline
                              J Offline
                              Jonathan de Halleux
                              wrote on last edited by
                              #14

                              I use foreach. :) Jonathan de Halleux - My Blog

                              R 1 Reply Last reply
                              0
                              • R Rick York

                                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.

                                D Offline
                                D Offline
                                Daniel Turini
                                wrote on last edited by
                                #15

                                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!

                                R 1 Reply Last reply
                                0
                                • P peterchen

                                  After a batch of interviews, the second most notable thing is that where both ++i and i++ are possible, all candidates used i++ 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's for(int i=0; i<n; ++i) , and Vote 1 if it's for(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

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #16

                                  ++i is safe there but a bad habit in other places so I stick with i++. Elaine :rose: The tigress is here :-D

                                  P 1 Reply Last reply
                                  0
                                  • J Jonathan de Halleux

                                    I use foreach. :) Jonathan de Halleux - My Blog

                                    R Offline
                                    R Offline
                                    roel_
                                    wrote on last edited by
                                    #17

                                    Spoken with wisdom :)

                                    1 Reply Last reply
                                    0
                                    • L Lost User

                                      ++i is safe there but a bad habit in other places so I stick with i++. Elaine :rose: The tigress is here :-D

                                      P Offline
                                      P Offline
                                      peterchen
                                      wrote on last edited by
                                      #18

                                      Trollslayer wrote: but a bad habit in other places where? (never encountered one)


                                      we are here to help each other get through this thing, whatever it is Vonnegut jr.
                                      sighist || Agile Programming | doxygen

                                      1 Reply Last reply
                                      0
                                      • C Christian Graus

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

                                        P Offline
                                        P Offline
                                        peterchen
                                        wrote on last edited by
                                        #19

                                        exactly my thought - dunno why you were voted down...


                                        we are here to help each other get through this thing, whatever it is Vonnegut jr.
                                        sighist || Agile Programming | doxygen

                                        C 1 Reply Last reply
                                        0
                                        • P peterchen

                                          After a batch of interviews, the second most notable thing is that where both ++i and i++ are possible, all candidates used i++ 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's for(int i=0; i<n; ++i) , and Vote 1 if it's for(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

                                          M Offline
                                          M Offline
                                          megaadam
                                          wrote on last edited by
                                          #20

                                          // real programmers use i = false ? 1 - i : 1 + i; :suss: _____________________________________ Action without thought is not action Action without emotion is not life

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups