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

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

Expand Messages
  • 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 1 of 5 , May 9, 2012
      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 2 of 5 , May 10, 2012
        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.