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

Re: Bad variable names

Expand Messages
  • Chris
    ... Apologies for ressurrecting an old thread. I spent some time diagnosing an issue today that would have been caught by JSLint if it had this feature. I d
    Message 1 of 9 , May 13, 2011
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "Rob Richardson" <erobrich@...> wrote:
      >
      > I grant that technically a global and local variable of the same name are
      > completely different, but I'd propose that it's very confusing to those
      > unfamiliar with the codebase when local variables are named identically to
      > global variables. If for no other reason than other programmers might get
      > confused, it seems better to error on the side of caution, prohibiting the
      > declaration of identically named local variables to globally defined
      > variables elsewhere in the file or variables defined in /*global */ or
      > global variables triggered by other options such as "browser". If nothing
      > else, I see value in the ability to opt into a warning when such an event
      > has occurred -- like the current list of global variables in the report.
      >
      > Rob

      Apologies for ressurrecting an old thread. I spent some time diagnosing an issue today that would have been caught by JSLint if it had this feature. I'd like to voice support: I think this would be a useful option.

      I have prepared a sample of code that currently passes JSLint, but would fail if this feature were implemented:

      var GLOBAL = (function () {
          "use strict";

          var module = {},
              toDelete = [];

          module.deletion = function (node) {
              var toDelete = []; // (snip: produce array of ID attributes in node)
              // (snip: perform work with local toDelete)
              return toDelete.length; // Oops, needed toDelete from closure scope
          };

          module.save = function () {
              toDelete.forEach(function (item) {
                  // (snip) perform deletion
              });
          };

          return module;
      }());

      As you can see, the "toDelete" variable in the "module.deletion" function had the same name as the "toDelete" variable in the closure scope.

      The same "module.deletion" function attempted to access the "toDelete" variable as it existed in both the local and the closure scopes. Obviously, this was the source of the bug.

      Had JSLint issued a warning about the redefinition of "toDelete", the bug would never have existed.

      While my example does not deal with the global scope, I think the same logic should apply.

      Thank you for your consideration!

      - Chris
    Your message has been successfully submitted and would be delivered to recipients shortly.