Best method to increment a variable.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    #1

    Best method to increment a variable.

    Several textbooks, including Javascript for Dummies (2005), show the
    "i=++i" method of incrementing a variable. I have been using this
    method but I have been recently informed by some experts in the Google
    Maps API group that this method is confusing and wasteful of resources
    and is now redundant and I should use "++i" or "i=i+1". Any strong
    opinions in this group?
  • AKS

    #2
    Re: Best method to increment a variable.



    Steve:
    and is now redundant and I should use "++i" or "i=i+1".

    I think that -prefix increment operator (++i)- is more effective and
    its action are clear enough.

    Comment

    • AKS

      #3
      Re: Best method to increment a variable.

      On 16 ÄÅË, 15:46, Steve <stephen.jo...@ googlemail.comw rote:
      ...and is now redundant and I should use "++i" or "i=i+1".
      I think that -prefix increment operator (++i)- is more effective and
      its actions are clear enough.

      Comment

      • The Natural Philosopher

        #4
        Re: Best method to increment a variable.

        AKS wrote:
        On 16 ���, 15:46, Steve <stephen.jo...@ googlemail.comw rote:
        >
        >...and is now redundant and I should use "++i" or "i=i+1".
        >
        I think that -prefix increment operator (++i)- is more effective and
        its actions are clear enough.
        >
        Dunno about Javascript, but when I examined the output of compiled C for

        i=i+1;
        i+=1;
        i++;
        ++i;

        They all resulted in exactly the same code.

        a simple register INC operator..;-)

        (or if 'i' was never referenced later, nothing at all. Modern compilers
        are very good at eliminating unused variables ;-))

        If there was many programmers outputting clean readable code that works,
        instead of arguing about the most elegant and efficient constructs, the
        world would probably be a better place.

        Comment

        • Doug Miller

          #5
          Re: Best method to increment a variable.

          In article <5ecc086d-5d4c-4350-9230-cc04bbcfda8f@b1 g2000pra.google groups.com>, Steve <stephen.joung@ googlemail.comw rote:
          >Several textbooks, including Javascript for Dummies (2005), show the
          >"i=++i" method of incrementing a variable.
          You sure about that? Or did you mean i = i+1 ?


          --
          Regards,
          Doug Miller (alphageek at milmac dot com)

          It's time to throw all their damned tea in the harbor again.

          Comment

          • Steve

            #6
            Re: Best method to increment a variable.

            On Dec 16, 2:03 pm, spamb...@milmac .com (Doug Miller) wrote:
            In article <5ecc086d-5d4c-4350-9230-cc04bbcfd...@b1 g2000pra.google groups.com>, Steve <stephen.jo...@ googlemail.comw rote:
            >
            Several textbooks, including Javascript for Dummies (2005), show the
            "i=++i" method of incrementing a variable.
            >
            You sure about that? Or did you mean i = i+1 ?
            Yes I'm sure, it is in the table on page 50 of the Dummies book for
            example, except they use x instead of i. And it works too, try it!

            Cheers, Steve.
            >
            --
            Regards,
            Doug Miller (alphageek at milmac dot com)
            >
            It's time to throw all their damned tea in the harbor again.

            Comment

            • The Natural Philosopher

              #7
              Re: Best method to increment a variable.

              Steve wrote:
              On Dec 16, 2:03 pm, spamb...@milmac .com (Doug Miller) wrote:
              >In article <5ecc086d-5d4c-4350-9230-cc04bbcfd...@b1 g2000pra.google groups.com>, Steve <stephen.jo...@ googlemail.comw rote:
              >>
              >>Several textbooks, including Javascript for Dummies (2005), show the
              >>"i=++i" method of incrementing a variable.
              >You sure about that? Or did you mean i = i+1 ?
              >
              Yes I'm sure, it is in the table on page 50 of the Dummies book for
              example, except they use x instead of i. And it works too, try it!
              >
              Syntactically it makes sense.Increment 'i' *before* assigning its value
              to 'i'...

              But its unnecessarily verbose.

              5 characters, same as i=i+1.

              I always prefer i+=1; Four characters.

              But i++; is 3, as is ++i;

              <shrug>

              Cheers, Steve.
              >--
              >Regards,
              > Doug Miller (alphageek at milmac dot com)
              >>
              >It's time to throw all their damned tea in the harbor again.
              >

              Comment

              • DS

                #8
                Re: Best method to increment a variable.

                On Sun, 16 Dec 2007 02:46:22 -0800, Steve wrote:
                Several textbooks, including Javascript for Dummies (2005), show the
                "i=++i" method of incrementing a variable. I have been using this method
                but I have been recently informed by some experts in the Google Maps API
                group that this method is confusing and wasteful of resources and is now
                redundant and I should use "++i" or "i=i+1". Any strong opinions in this
                group?
                Profile your code first! There is absolutely zero point in optimizing
                something which may have 1% or less performance degradation on your code.
                Instead, work at optimizing your code which consumes most of the CPU. Use
                firebug's profiler to determine the functions your code is spending most
                of its time.

                Without taking any direct measurements, I would assume i++ and ++i will
                be the most efficient when using most javascript engines because the
                other expressions will probably introduce temporary variables.

                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: Best method to increment a variable.

                  Steve wrote:
                  On Dec 16, 2:03 pm, spamb...@milmac .com (Doug Miller) wrote:
                  >In article <5ecc086d-5d4c-4350-9230-cc04bbcfd...@b1 g2000pra.google groups.com>, Steve <stephen.jo...@ googlemail.comw rote:
                  >>Several textbooks, including Javascript for Dummies (2005), show the
                  >>"i=++i" method of incrementing a variable.
                  >You sure about that? Or did you mean i = i+1 ?
                  >
                  Yes I'm sure, it is in the table on page 50 of the Dummies book for
                  example, except they use x instead of i. [...]
                  Hopefully you have a shredder.


                  PointedEars
                  --
                  var bugRiddenCrashP ronePieceOfJunk = (
                  navigator.userA gent.indexOf('M SIE 5') != -1
                  && navigator.userA gent.indexOf('M ac') != -1
                  ) // Plone, register_functi on.js:16

                  Comment

                  • Lee

                    #10
                    Re: Best method to increment a variable.

                    Steve said:
                    >
                    >On Dec 16, 2:03 pm, spamb...@milmac .com (Doug Miller) wrote:
                    >>In article <5ecc086d-5d4c-4350-9230-cc04bbcfd...@b1 g2000pra.google groups.com>,
                    >>Steve <stephen.jo...@ googlemail.comw rote:
                    >>
                    >Several textbooks, including Javascript for Dummies (2005), show the
                    >"i=++i" method of incrementing a variable.
                    >>
                    >You sure about that? Or did you mean i = i+1 ?
                    >
                    >Yes I'm sure, it is in the table on page 50 of the Dummies book for
                    >example, except they use x instead of i. And it works too, try it!

                    i=++i;

                    is equivalent to the two lines:

                    i++;
                    i=i;

                    So, yes, it works, but it's silly to do it that way.


                    --

                    Comment

                    • Evertjan.

                      #11
                      Re: Best method to increment a variable.

                      Lee wrote on 16 dec 2007 in comp.lang.javas cript:
                      i=++i;
                      >
                      is equivalent to the two lines:
                      >
                      i++;
                      i=i;
                      >
                      So, yes, it works, but it's silly to do it that way.
                      Try figuring out [without testing]
                      what the alerted result of this would be:

                      var i = 6;
                      i = i++;
                      alert(i);

                      And now of this:

                      var i = 6;
                      i = (++i + i--)/2;
                      alert(i);

                      And now test it. ;-)

                      --
                      Evertjan.
                      The Netherlands.
                      (Please change the x'es to dots in my emailaddress)

                      Comment

                      • Richard Cornford

                        #12
                        Re: Best method to increment a variable.

                        Steve wrote:
                        On Dec 16, 2:03 pm, Doug Miller wrote:
                        >Steve wrote:
                        >>Several textbooks, including Javascript for Dummies (2005),
                        >>show the "i=++i" method of incrementing a variable.
                        >>
                        >You sure about that? Or did you mean i = i+1 ?
                        >
                        Yes I'm sure, it is in the table on page 50 of the Dummies
                        book for example, except they use x instead of i. And it
                        works too, try it!
                        <snip>

                        The 'it works' criteria only gets you so far. The outcome of - i = ++
                        i; - is not different from - ++i; -, but the second operation in the
                        expression mans that the first will take (very fractionally) longer and
                        if the outcomes are the same that second operation must be redundant.

                        Remember that books on javascript are more likely to be written by
                        journalists than programmers, and even if written by programmers they
                        are unlikely to be good javascript programmers. David Flanagan's
                        "Javascript : The definitive Guide" is, by a very wide margin, the best
                        (or, more normally, the least bad) book available on javascript, but it
                        is quite obvious from its text that even its author has little practical
                        experience of browser scripting (certainly in comparison to his
                        experience of writing books) and in many cases is just parroting the
                        assertions of other, less then well informed, sources.

                        Richard.

                        Comment

                        • Steve

                          #13
                          Re: Best method to increment a variable.

                          On Dec 16, 8:59 pm, "Richard Cornford" <Rich...@litote s.demon.co.uk>
                          wrote:
                          Steve wrote:
                          On Dec 16, 2:03 pm, Doug Miller wrote:
                          Steve wrote:
                          >Several textbooks, including Javascript for Dummies (2005),
                          >show the "i=++i" method of incrementing a variable.
                          >
                          You sure about that? Or did you mean i = i+1 ?
                          >
                          Yes I'm sure, it is in the table on page 50 of the Dummies
                          book for example, except they use x instead of i. And it
                          works too, try it!
                          >
                          <snip>
                          >
                          The 'it works' criteria only gets you so far. The outcome of - i = ++
                          i; - is not different from - ++i; -, but the second operation in the
                          expression mans that the first will take (very fractionally) longer and
                          if the outcomes are the same that second operation must be redundant.
                          >
                          Remember that books on javascript are more likely to be written by
                          journalists than programmers, and even if written by programmers they
                          are unlikely to be good javascript programmers. David Flanagan's
                          "Javascript : The definitive Guide" is, by a very wide margin, the best
                          (or, more normally, the least bad) book available on javascript, but it
                          is quite obvious from its text that even its author has little practical
                          experience of browser scripting (certainly in comparison to his
                          experience of writing books) and in many cases is just parroting the
                          assertions of other, less then well informed, sources.
                          >
                          Richard.
                          So where did the good javascript programmers learn to program
                          javascript?

                          Steve

                          Comment

                          • Dr J R Stockton

                            #14
                            Re: Best method to increment a variable.

                            In comp.lang.javas cript message <13macj6927jut0 f@corp.supernew s.com>,
                            Sun, 16 Dec 2007 14:12:54, DS <ds@bigpond.net posted:
                            >On Sun, 16 Dec 2007 02:46:22 -0800, Steve wrote:
                            >
                            >Several textbooks, including Javascript for Dummies (2005), show the
                            >"i=++i" method of incrementing a variable. I have been using this method
                            >but I have been recently informed by some experts in the Google Maps API
                            >group that this method is confusing and wasteful of resources and is now
                            >redundant and I should use "++i" or "i=i+1". Any strong opinions in this
                            >group?
                            >
                            >Profile your code first! There is absolutely zero point in optimizing
                            >something which may have 1% or less performance degradation on your code.
                            >Instead, work at optimizing your code which consumes most of the CPU. Use
                            >firebug's profiler to determine the functions your code is spending most
                            >of its time.
                            (1) It is even more important, where applicable, to optimise the
                            underlying algorithm that the code represents.

                            (2) While the difference in run speed between i++ ++i i+=1 and i=i+1
                            will except in artificial cases be negligible, the exercise of
                            considering and perhaps testing it may well lead to improved
                            understanding of how the language works.

                            It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

                            --
                            (c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6
                            news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
                            <URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
                            <URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.

                            Comment

                            • Dr J R Stockton

                              #15
                              Re: Best method to increment a variable.

                              In comp.lang.javas cript message <eba4d857-4c70-47e7-a3a2-f0f30c56a3ba@e2
                              3g2000prf.googl egroups.com>, Sun, 16 Dec 2007 13:09:06, Steve
                              <stephen.joung@ googlemail.comp osted:
                              >
                              >So where did the good javascript programmers learn to program
                              >javascript?
                              They will have learned by applying a combination of experience,
                              intelligence, and suspicion to the available source materials.

                              Note that an intelligent five-year-old should be able to detect at least
                              one error in ECMA-262 3rd Edn, which therefore cannot be considered
                              infallible.

                              --
                              (c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6
                              news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
                              <URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
                              <URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.

                              Comment

                              Working...