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

RE: [jslint] Nested loops with the same control vars

Expand Messages
  • Rob Richardson
    I think an even larger issue should be checking for identically named variables declared in different scope. This too should be illegal: function a() {
    Message 1 of 5 , May 9, 2012
    • 0 Attachment
      I think an even larger issue should be checking for identically named
      variables declared in different scope. This too should be illegal:

      function a() {
      ````var i;
      ````function b() {
      ````````var i;
      ````};
      };


      -----Original Message-----
      From: jslint_com@yahoogroups.com [mailto:jslint_com@yahoogroups.com] On
      Behalf Of Max
      Sent: Wednesday, May 09, 2012 9:57 AM
      To: jslint_com@yahoogroups.com
      Subject: Re: [jslint] Nested loops with the same control vars



      What might be the general rule be, just literally "Nested for loops
      shouldn't share control variables"? Or is there a larger principle at play
      here?

      On 5/9/2012 9:00 AM, Martin Cooper wrote:
      >
      > I was surprised to find that JSLint doesn't complain about the
      > following code:
      >
      > function doCheck(arr1, arr2, checker) { var i, l; for (i = 0, l =
      > arr1.length; i < l; i = i + 1) { for (i = 0, l = arr2.length; i < l; i
      > = i + 1) { checker(arr1[i], arr2[i]); } } }
      >
      > I expected it to catch the fact that 'i' and 'l' are being used as
      > control vars in both loops, the inner one messing up the state of the
      > outer one.
      >
      > Seems like this would be a reasonable check for JSLint to have. I
      > can't think of a situation in which this code structure would be a
      > good thing.
      >
      > Thoughts?
      >
      > --
      > Martin Cooper
      >
      > __._,_.__
    • John Hawkinson
      Rob Richardson wrote on Wed, 9 May 2012 ... I don t agree. i is a common counter variable. It is reasonable style to use i as the
      Message 2 of 5 , May 9, 2012
      • 0 Attachment
        Rob Richardson <erobrich@...> wrote on Wed, 9 May 2012
        at 11:16:11 -0700 in <003a01cd2e0f$d0f95240$72ebf6c0$@...>:

        > I think an even larger issue should be checking for identically named
        > variables declared in different scope.

        I don't agree.

        i is a common counter variable. It is reasonable style to use i
        as the primary counter variable in all non-nested for loops.
        It's quite fine for an outer function to use 'i' as counter for its
        for loops even when an inner helper function also uses' i' as a counter
        for its for loops. In fact, arguably it is best -- otherwise it means
        the programmer has to worry about what all the helper functions are
        within an outer function and what all their variables are. And if they
        must be treated as coming from the same space, it's almost like we
        didn't have nested function scope at all!

        I think it would be reasonable to warn when a function-local variable
        masks another variable iwth the same name, because it certainly can
        be an error. But it need not be, and non-nested for loop counter
        variables, I don't think it's an error, I think its actually good style.

        I do not want to think about what all the local variables used by the
        functions I call are. And I should not have to.

        --jhawk@...
        John Hawkinson

        > This too should be illegal:
        >
        > function a() {
        > ````var i;
        > ````function b() {
        > ````````var i;
        > ````};
        > };
      • Chris
        I agree: I think such a rule would be a useful improvement to JSLint. I have seen this sort of situation causing numerous bugs in my own code. Of course, we
        Message 3 of 5 , May 10, 2012
        • 0 Attachment
          I agree: I think such a rule would be a useful improvement to JSLint. I have seen this sort of situation causing numerous bugs in my own code. Of course, we have discussed this before:

          http://tech.groups.yahoo.com/group/jslint_com/message/2212

          Also, some time later, I found a bug in JSLint itself that was caused by this coding practice:
          http://tech.groups.yahoo.com/group/jslint_com/message/2372

          When you have nested scopes, the idea of declaring variables in an outer scope and redeclaring them in an inner scope is naturally confusing. I argue that any code written in this way could be written more clearly by giving the variables distinct names.

          - Chris

          --- In jslint_com@yahoogroups.com, "Rob Richardson" <erobrich@...> wrote:
          >
          > I think an even larger issue should be checking for identically named
          > variables declared in different scope. This too should be illegal:
          >
          > function a() {
          > ````var i;
          > ````function b() {
          > ````````var i;
          > ````};
          > };
        Your message has been successfully submitted and would be delivered to recipients shortly.