Loading ...
Sorry, an error occurred while loading the content.

init of for

Expand Messages
  • Heinz Rasched
    I like to give my vars a value when I declare them, whenever that is possible. JsLint does not allow me to do that in one special case: function f(an_array) {
    Message 1 of 8 , Feb 25, 2013
    • 0 Attachment
      I like to give my vars a value when I declare them,
      whenever that is possible.

      JsLint does not allow me to do that in one special case:

      function f(an_array) {
      'use strict'
      var i = 0,
      n = an_array.length;
      for (; i < n; i += 1) {
      // operate on an_array[i]
      }
      }

      It complains about the missing initial expression in the
      for statement.
      So I am forced to write

      var i = 0,
      n = an_array.length;
      for (i = 0; [...]

      and whence repeating myself

      or to leave i not initialized and go with the common

      var i,
      n;
      for (i = 0, n = an_array.length; [...]


      Well, not a big deal, but it makes me sad.
    • douglascrockford
      ... (; looks like a mistake. Do not use forms that look like mistakes. To make it easier to find the needle, make your program look less like a haystack.
      Message 2 of 8 , Feb 25, 2013
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "Heinz Rasched" <raschedh@...> wrote:
        >
        > I like to give my vars a value when I declare them,
        > whenever that is possible.
        >
        > JsLint does not allow me to do that in one special case:
        >
        > function f(an_array) {
        > 'use strict'
        > var i = 0,
        > n = an_array.length;
        > for (; i < n; i += 1) {
        > // operate on an_array[i]
        > }
        > }
        >
        > It complains about the missing initial expression in the
        > for statement.
        > So I am forced to write
        >
        > var i = 0,
        > n = an_array.length;
        > for (i = 0; [...]
        >
        > and whence repeating myself
        >
        > or to leave i not initialized and go with the common
        >
        > var i,
        > n;
        > for (i = 0, n = an_array.length; [...]
        >
        >
        > Well, not a big deal, but it makes me sad.


        (; looks like a mistake. Do not use forms that look like mistakes.

        To make it easier to find the needle, make your program look less like a haystack.
      • Heinz Rasched
        I knew that you wouldn t like that. ... That depends on the context of the `(; ... I do not. ... My programs are far from looking like that. I never look for
        Message 3 of 8 , Feb 25, 2013
        • 0 Attachment
          I knew that you wouldn't like that.

          > (; looks like a mistake.

          That depends on the context of the `(;'

          > Do not use forms that look like mistakes.

          I do not.

          > To make it easier to find the needle, make your program look less like a haystack.

          My programs are far from looking like that.
          I never look for needles in them.

          Anyhow, thanks for the consideration.

          Of course, I'll pledge myself to JsLint's teachings,
          and will keep on writing

          (i = 0, n = heretic.length; [...]
        • John Hawkinson
          Heinz Rasched wrote on Mon, 25 Feb 2013 ... From an idempotency perspective, you re better off with the the initialization in the for loop
          Message 4 of 8 , Feb 25, 2013
          • 0 Attachment
            Heinz Rasched <raschedh@...> wrote on Mon, 25 Feb 2013
            at 12:37:13 -0000 in <kgfltp+agd7@...>:

            > function f(an_array) {
            > 'use strict'
            > var i = 0,
            > n = an_array.length;
            > for (; i < n; i += 1) {
            > // operate on an_array[i]
            > }
            > }

            From an idempotency perspective, you're better off with the
            the initialization in the for loop anyhow. Because it's common
            for functinos to be long enough that review of all the code
            between initialization and loop may be burdensome, as is reuse
            of loop variables in multiple loops. If you therefore had:

            var i=0;
            ...
            for (; i<n; i +=1 ) { /* some loop */ }
            ...
            for (; i<n; i +=1 ) { /* some other loop*/ }

            then i would be zero for the first for() but not the second.


            It's nice for the for loop to be self contained. One can look
            at the loop itself and immediately understand it, without having
            to worry about initialization that might be far away from it.


            JavaScript's lack of block scope really suggests that the variables
            you should initialize at the top of a function are those whose value
            is expected to stay constant throughout the function.


            This sort of generalization matters more for longer functions, of
            course.

            --jhawk@...
            John Hawkinson
          • Jonas Trollvik
            What is the benefit of caching the array length here? I doubt it has any effect on performance. function f(an_array) { use strict var i; for (i = 0; i
            Message 5 of 8 , Feb 25, 2013
            • 0 Attachment
              What is the benefit of caching the array length here?

              I doubt it has any effect on performance.

              function f(an_array) {
              'use strict'
              var i;
              for (i = 0; i < an_array.length; i += 1) {
              // operate on an_array[i]
              }
              }

              looks like it is a lot cleaner and easier to read.

              2013/2/25 Heinz Rasched <raschedh@...>

              > **
              >
              >
              > I knew that you wouldn't like that.
              >
              >
              > > (; looks like a mistake.
              >
              > That depends on the context of the `(;'
              >
              >
              > > Do not use forms that look like mistakes.
              >
              > I do not.
              >
              >
              > > To make it easier to find the needle, make your program look less like a
              > haystack.
              >
              > My programs are far from looking like that.
              > I never look for needles in them.
              >
              > Anyhow, thanks for the consideration.
              >
              > Of course, I'll pledge myself to JsLint's teachings,
              > and will keep on writing
              >
              > (i = 0, n = heretic.length; [...]
              >
              >
              >


              [Non-text portions of this message have been removed]
            • Mike On Mobile
              At one time it was considered best practice to store the array length in a local variable and use that in the comparison . That was because the object property
              Message 6 of 8 , Feb 25, 2013
              • 0 Attachment
                At one time it was considered best practice to store the array length in a local variable and use that in the comparison . That was because the object property lookup was significantly more expensive than using the local variable. However, recent JavaScript engines such as v8 reduce the performance gap so greatly that the benefit of this optimization is negligible. But it still makes a big difference in ie8 iirc.

                Jonas Trollvik <jonas@...> wrote:

                >What is the benefit of caching the array length here?
                >
                >I doubt it has any effect on performance.
                >
                >function f(an_array) {
                >'use strict'
                >var i;
                >for (i = 0; i < an_array.length; i += 1) {
                >// operate on an_array[i]
                >}
                >}
                >
                >looks like it is a lot cleaner and easier to read.
                >
                >2013/2/25 Heinz Rasched <raschedh@...>
                >
                >> **
                >>
                >>
                >> I knew that you wouldn't like that.
                >>
                >>
                >> > (; looks like a mistake.
                >>
                >> That depends on the context of the `(;'
                >>
                >>
                >> > Do not use forms that look like mistakes.
                >>
                >> I do not.
                >>
                >>
                >> > To make it easier to find the needle, make your program look less like a
                >> haystack.
                >>
                >> My programs are far from looking like that.
                >> I never look for needles in them.
                >>
                >> Anyhow, thanks for the consideration.
                >>
                >> Of course, I'll pledge myself to JsLint's teachings,
                >> and will keep on writing
                >>
                >> (i = 0, n = heretic.length; [...]
                >>
                >>
                >>
                >
                >
                >[Non-text portions of this message have been removed]
                >
                >
                >
                >------------------------------------
                >
                >Yahoo! Groups Links
                >
                >
                >
              • Marcel Duran
                That s true, however html collections are considered live which means they re automatically updated when the underlying document is changed. ... --
                Message 7 of 8 , Feb 25, 2013
                • 0 Attachment
                  That's true, however html collections are considered live which means
                  they're automatically updated when the underlying document is changed.

                  On Monday, February 25, 2013, Mike On Mobile wrote:

                  > **
                  >
                  >
                  > At one time it was considered best practice to store the array length in a
                  > local variable and use that in the comparison . That was because the object
                  > property lookup was significantly more expensive than using the local
                  > variable. However, recent JavaScript engines such as v8 reduce the
                  > performance gap so greatly that the benefit of this optimization is
                  > negligible. But it still makes a big difference in ie8 iirc.
                  >
                  > Jonas Trollvik jonas@... <javascript:_e({}, 'cvml',
                  > 'jonas%40weightpoint.se');>> wrote:
                  >
                  > >What is the benefit of caching the array length here?
                  > >
                  > >I doubt it has any effect on performance.
                  > >
                  > >function f(an_array) {
                  > >'use strict'
                  > >var i;
                  > >for (i = 0; i < an_array.length; i += 1) {
                  > >// operate on an_array[i]
                  > >}
                  > >}
                  > >
                  > >looks like it is a lot cleaner and easier to read.
                  > >
                  > >2013/2/25 Heinz Rasched raschedh@... <javascript:_e({}, 'cvml',
                  > 'raschedh%40yahoo.com');>>
                  > >
                  > >> **
                  > >>
                  > >>
                  > >> I knew that you wouldn't like that.
                  > >>
                  > >>
                  > >> > (; looks like a mistake.
                  > >>
                  > >> That depends on the context of the `(;'
                  > >>
                  > >>
                  > >> > Do not use forms that look like mistakes.
                  > >>
                  > >> I do not.
                  > >>
                  > >>
                  > >> > To make it easier to find the needle, make your program look less
                  > like a
                  > >> haystack.
                  > >>
                  > >> My programs are far from looking like that.
                  > >> I never look for needles in them.
                  > >>
                  > >> Anyhow, thanks for the consideration.
                  > >>
                  > >> Of course, I'll pledge myself to JsLint's teachings,
                  > >> and will keep on writing
                  > >>
                  > >> (i = 0, n = heretic.length; [...]
                  > >>
                  > >>
                  > >>
                  > >
                  > >
                  > >[Non-text portions of this message have been removed]
                  > >
                  > >
                  > >
                  > >------------------------------------
                  > >
                  > >Yahoo! Groups Links
                  > >
                  > >
                  > >
                  >
                  >


                  --
                  @marcelduran


                  [Non-text portions of this message have been removed]
                • Heinz Rasched
                  ... Because having been blamed with your usual accusations yada yada, I take the liberty to thank you for the context coloring stuff in jslint ! Not because i
                  Message 8 of 8 , Feb 26, 2013
                  • 0 Attachment
                    > (; looks like a mistake. Do not use forms that look like mistakes.
                    >
                    > To make it easier to find the needle, make your program look less like a haystack.

                    Because having been blamed with your usual accusations yada yada,
                    I take the liberty to thank you for the context coloring stuff in jslint !

                    Not because i use it, but because of the heresy of yours, that
                    the usual syntax highlighting is for children, which I found
                    an inspiring thought to play with in my mind.

                    So I switched mine off for all languages a month ago.

                    I'll never switch it on again.

                    Thank you !
                  Your message has been successfully submitted and would be delivered to recipients shortly.