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

Bad variable names

Expand Messages
  • Ezequiel
    For example, someone could innocently do: (function () { var String = cool story, bro ; // Later in our program... if (!String.prototype.trim) { // ... }
    Message 1 of 9 , Jan 23, 2011
    • 0 Attachment
      For example, someone could innocently do:

      (function () {
      var String = 'cool story, bro';

      // Later in our program...
      if (!String.prototype.trim) {
      // ...
      }
      }());

      JSLint says this is fine. However, this snippet will produce an error because we chose to name our variable "String," and now String.prototype is totally inaccessible. It would be nice if we were warned because of this.
    • Douglas Crockford
      ... Is there any evidence or experience to show that this would ever detect a real problem?
      Message 2 of 9 , Jan 24, 2011
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "Ezequiel" <thehungerforce@...> wrote:
        >
        > For example, someone could innocently do:
        >
        > (function () {
        > var String = 'cool story, bro';
        >
        > // Later in our program...
        > if (!String.prototype.trim) {
        > // ...
        > }
        > }());
        >
        > JSLint says this is fine. However, this snippet will produce an error because we chose to name our variable "String," and now String.prototype is totally inaccessible. It would be nice if we were warned because of this.


        Is there any evidence or experience to show that this would ever detect a real problem?
      • abyssoft@ymail.com
        ... Primitives should not be permitted to be redefined either at Global or anon levels. It is easy enough to accidentally use String versus string; Although I
        Message 3 of 9 , Jan 24, 2011
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
          >
          > --- In jslint_com@yahoogroups.com, "Ezequiel" <thehungerforce@> wrote:
          > >
          > > For example, someone could innocently do:
          > >
          > > (function () {
          > > var String = 'cool story, bro';
          > >
          > > // Later in our program...
          > > if (!String.prototype.trim) {
          > > // ...
          > > }
          > > }());
          > >
          > > JSLint says this is fine. However, this snippet will produce an error because we chose to name our variable "String," and now String.prototype is totally inaccessible. It would be nice if we were warned because of this.
          >
          >
          > Is there any evidence or experience to show that this would ever detect a real problem?
          >

          Primitives should not be permitted to be redefined either at Global or anon levels. It is easy enough to accidentally use String versus string; Although I recommend against using anything too close to primitives as variable names as it is a poor coding practice. I tend to use purposeful naming to all my variable names, applying the correct type of camel case. I hate having to remember what some cryptic variable name is for. I have enough trouble remembering my own name. j/k
        • Douglas Crockford
          ... I get flooded with requests to dumb JSLint down from people insisting that their bad practices are good. The thing I can claim about JSLint is that I have
          Message 4 of 9 , Jan 24, 2011
          • 0 Attachment
            --- In jslint_com@yahoogroups.com, "abyssoft@..." <abyssoft@...> wrote:
            >
            >
            > --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@> wrote:
            > >
            > > --- In jslint_com@yahoogroups.com, "Ezequiel" <thehungerforce@> wrote:
            > > >
            > > > For example, someone could innocently do:
            > > >
            > > > (function () {
            > > > var String = 'cool story, bro';
            > > >
            > > > // Later in our program...
            > > > if (!String.prototype.trim) {
            > > > // ...
            > > > }
            > > > }());
            > > >
            > > > JSLint says this is fine. However, this snippet will produce an error because we chose to name our variable "String," and now String.prototype is totally inaccessible. It would be nice if we were warned because of this.
            > >
            > >
            > > Is there any evidence or experience to show that this would ever detect a real problem?
            > >
            >
            > Primitives should not be permitted to be redefined either at Global or anon levels. It is easy enough to accidentally use String versus string; Although I recommend against using anything too close to primitives as variable names as it is a poor coding practice. I tend to use purposeful naming to all my variable names, applying the correct type of camel case. I hate having to remember what some cryptic variable name is for. I have enough trouble remembering my own name. j/k


            I get flooded with requests to dumb JSLint down from people insisting that their bad practices are good. The thing I can claim about JSLint is that I have seen problems coming from all of the things it complains about. So I will not consider SHOULD NOT BE PERMITTED arguments unless they are backed up with some sort of anecdote that the thing that should not be permitted actually caused a bug once.
          • Ezequiel
            Well, JSLint already has this. ie. var String = ; = Problem at line 1 character 5: Redefinition of String . JSLint doesn t see this as an issue if we do it
            Message 5 of 9 , Jan 25, 2011
            • 0 Attachment
              Well, JSLint already has this.

              ie.

              var String = '';

              => Problem at line 1 character 5: Redefinition of 'String'.

              JSLint doesn't see this as an issue if we do it inside a function, however.

              Whether you initialize that var inside or outside a function, it still has the same effect of redefining "String."


              --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
              >
              > --- In jslint_com@yahoogroups.com, "abyssoft@" <abyssoft@> wrote:
              > >
              > >
              > > --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@> wrote:
              > > >
              > > > --- In jslint_com@yahoogroups.com, "Ezequiel" <thehungerforce@> wrote:
              > > > >
              > > > > For example, someone could innocently do:
              > > > >
              > > > > (function () {
              > > > > var String = 'cool story, bro';
              > > > >
              > > > > // Later in our program...
              > > > > if (!String.prototype.trim) {
              > > > > // ...
              > > > > }
              > > > > }());
              > > > >
              > > > > JSLint says this is fine. However, this snippet will produce an error because we chose to name our variable "String," and now String.prototype is totally inaccessible. It would be nice if we were warned because of this.
              > > >
              > > >
              > > > Is there any evidence or experience to show that this would ever detect a real problem?
              > > >
              > >
              > > Primitives should not be permitted to be redefined either at Global or anon levels. It is easy enough to accidentally use String versus string; Although I recommend against using anything too close to primitives as variable names as it is a poor coding practice. I tend to use purposeful naming to all my variable names, applying the correct type of camel case. I hate having to remember what some cryptic variable name is for. I have enough trouble remembering my own name. j/k
              >
              >
              > I get flooded with requests to dumb JSLint down from people insisting that their bad practices are good. The thing I can claim about JSLint is that I have seen problems coming from all of the things it complains about. So I will not consider SHOULD NOT BE PERMITTED arguments unless they are backed up with some sort of anecdote that the thing that should not be permitted actually caused a bug once.
              >
            • Douglas Crockford
              ... That s not true at all. var String inside of a function declares a local variable that can only be seen by the code you put into that function with it.
              Message 6 of 9 , Jan 25, 2011
              • 0 Attachment
                --- In jslint_com@yahoogroups.com, "Ezequiel" <thehungerforce@...> wrote:
                > Well, JSLint already has this.
                >
                > ie.
                >
                > var String = '';
                >
                > => Problem at line 1 character 5: Redefinition of 'String'.
                >
                > JSLint doesn't see this as an issue if we do it inside a function, however.
                >
                > Whether you initialize that var inside or outside a function, it still has the same effect of redefining "String."


                That's not true at all. var String inside of a function declares a local variable that can only be seen by the code you put into that function with it. That is nothing like redefining a primordial global variable.
              • Ezequiel
                All right, but if I do that, I still can t access String.prototype for some reason.
                Message 7 of 9 , Jan 25, 2011
                • 0 Attachment
                  All right, but if I do that, I still can't access String.prototype for some reason.

                  --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
                  >
                  > --- In jslint_com@yahoogroups.com, "Ezequiel" <thehungerforce@> wrote:
                  > > Well, JSLint already has this.
                  > >
                  > > ie.
                  > >
                  > > var String = '';
                  > >
                  > > => Problem at line 1 character 5: Redefinition of 'String'.
                  > >
                  > > JSLint doesn't see this as an issue if we do it inside a function, however.
                  > >
                  > > Whether you initialize that var inside or outside a function, it still has the same effect of redefining "String."
                  >
                  >
                  > That's not true at all. var String inside of a function declares a local variable that can only be seen by the code you put into that function with it. That is nothing like redefining a primordial global variable.
                  >
                • Rob Richardson
                  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
                  Message 8 of 9 , Jan 25, 2011
                  • 0 Attachment
                    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


                    -----Original Message-----
                    From: jslint_com@yahoogroups.com [mailto:jslint_com@yahoogroups.com] On
                    Behalf Of Douglas Crockford
                    Sent: Tuesday, January 25, 2011 2:32 PM
                    To: jslint_com@yahoogroups.com
                    Subject: [jslint] Re: Bad variable names

                    --- In jslint_com@yahoogroups.com, "Ezequiel" <thehungerforce@...> wrote:
                    > Well, JSLint already has this.
                    >
                    > ie.
                    >
                    > var String = '';
                    >
                    > => Problem at line 1 character 5: Redefinition of 'String'.
                    >
                    > JSLint doesn't see this as an issue if we do it inside a function,
                    however.
                    >
                    > Whether you initialize that var inside or outside a function, it still has
                    the same effect of redefining "String."


                    That's not true at all. var String inside of a function declares a local
                    variable that can only be seen by the code you put into that function with
                    it. That is nothing like redefining a primordial global variable.
                  • 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 9 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.