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

"Too many var statements"

Expand Messages
  • aseem.kishore@ymail.com
    Hi there, When I input this: function hello() { var x = 0; var y = 2; return x + y; } I get this error: Problem at line 3 character 9: Too many var statements.
    Message 1 of 6 , Aug 27, 2009
      Hi there,

      When I input this:

      function hello() {
      var x = 0;
      var y = 2;

      return x + y;
      }

      I get this error:

      Problem at line 3 character 9: Too many var statements.
      var y = 2;

      I can resolve the error by combining the two vars into one:

      function hello() {
      var x = 0,
      y = 2;

      return x + y;
      }

      But I'm not seeing why the latter is better. It seems to me the opposite; combining them is less explicit and requires careful attention to the commas, and thus more error prone.

      In fact, your own code conventions seem to say the same thing: "It is preferred that each variable be given its own line and comment." (via http://javascript.crockford.com/code.html#variable%20declarations)

      So is this a bug in JSLint? Or has your philosophy changed? (And if so, why?)

      Thanks!

      Aseem
    • douglascrockford
      ... Having only one var statement per function reduces code size, and also reduces the likelihood of block scope confusion. If that doesn t work for you, then
      Message 2 of 6 , Aug 27, 2009
        --- In jslint_com@yahoogroups.com, "aseem.kishore@..." <aseem.kishore@...> wrote:
        > When I input this:
        >
        > function hello() {
        > var x = 0;
        > var y = 2;
        >
        > return x + y;
        > }
        >
        > I get this error:
        >
        > Problem at line 3 character 9: Too many var statements.
        > var y = 2;
        >
        > I can resolve the error by combining the two vars into one:
        >
        > function hello() {
        > var x = 0,
        > y = 2;
        >
        > return x + y;
        > }
        >
        > But I'm not seeing why the latter is better. It seems to me the opposite; combining them is less explicit and requires careful attention to the commas, and thus more error prone.
        >
        > In fact, your own code conventions seem to say the same thing: "It is preferred that each variable be given its own line and comment." (via http://javascript.crockford.com/code.html#variable%20declarations)
        >
        > So is this a bug in JSLint? Or has your philosophy changed? (And if
        > so, why?)

        Having only one var statement per function reduces code size, and also reduces the likelihood of block scope confusion.

        If that doesn't work for you, then don't check the Allow one var statement per function option.
      • aseem.kishore@ymail.com
        ... Sorry, I did miss that option. Personally, I don t feel that the pro of code size outweighs the con of readability and maintenance in this case. Given
        Message 3 of 6 , Aug 27, 2009
          --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@...> wrote:
          >
          > Having only one var statement per function reduces code size, and also reduces the likelihood of block scope confusion.
          >
          > If that doesn't work for you, then don't check the Allow one var statement per function option.
          >

          Sorry, I did miss that option.

          Personally, I don't feel that the pro of code size outweighs the con of readability and maintenance in this case. Given several minification options as well as gzip, aren't we taught to not worry about code size when it affects readability like this? (Are extra vars, four bytes at a time, really going to matter?)

          That's just my opinion though, and I can see that reduced code size is a benefit. What I'm not seeing is why the check for incorrectly-block-scoped vars needs to be tied to the check for one var per function.

          When I uncheck the onevar option and enter this:

          function hello() {
          var x = 1;
          var y = 2;

          if (y > x) {
          var z = 3;
          x += z;
          }

          return x + y;
          }

          It passes, even though "var z" inside the if {} block is clearly misleading.

          I wish I didn't have to choose between both checks or none. Please take that into consideration. =)

          Thanks!

          Aseem
        • marcelduran
          ... minification won t reduce the code size as var is a reserved word.
          Message 4 of 6 , Aug 27, 2009
            > Personally, I don't feel that the pro of code size outweighs the con of readability and maintenance in this case. Given several minification options as well as gzip, aren't we taught to not worry about code size when it affects readability like this? (Are extra vars, four bytes at a time, really going to matter?)

            minification won't reduce the code size as var is a reserved word.
          • aseem.kishore@ymail.com
            ... Yes, I realize. But how much extra size do you think var statements add up to for a pre-minification 100K+ library? A library I author is 75.3 KB
            Message 5 of 6 , Aug 27, 2009
              --- In jslint_com@yahoogroups.com, "marcelduran" <contact@...> wrote:
              >
              > > Personally, I don't feel that the pro of code size outweighs the con of readability and maintenance in this case. Given several minification options as well as gzip, aren't we taught to not worry about code size when it affects readability like this? (Are extra vars, four bytes at a time, really going to matter?)
              >
              > minification won't reduce the code size as var is a reserved word.
              >

              Yes, I realize. But how much extra size do you think "var " statements add up to for a pre-minification 100K+ library?

              A library I author is 75.3 KB post-JSMin, pre-gzip. I did a count of "var " in the entire thing and got 534. If I could magically combine all of them into one, I would only cut 2 KB. After gzipping, that 2 KB makes virtually no difference.

              http://www.google.com/search?q=75.3+KB+-+534+*+4+bytes+%3D
            • Stefan Weiss
              ... That s not what this option does (and there is no block scope in JavaScript). ... The onevar option doesn t check for this at all. The following code
              Message 6 of 6 , Aug 27, 2009
                On 27/08/09 20:09, aseem.kishore@... wrote:
                > What I'm not seeing is why the check for incorrectly-block-scoped
                > vars needs to be tied to the check for one var per function.

                That's not what this option does (and there is no block scope in
                JavaScript).

                > When I uncheck the onevar option and enter this:
                >
                > function hello() {
                > var x = 1;
                > var y = 2;
                >
                > if (y > x) {
                > var z = 3;
                > x += z;
                > }
                >
                > return x + y;
                > }
                >
                > It passes, even though "var z" inside the if {} block is clearly misleading.

                The "onevar" option doesn't check for this at all. The following code
                would also pass, with or without the option enabled:

                function hello() {
                if (false) {
                var z = 3;
                }
                return z;
                }

                Technically, there's nothing wrong with the "var z". What may be
                confusing is that z will be declared regardless of the value in the
                if-condition, but the assignment will only be evaluated if the condition
                is true.

                I find the onevar option to be useful in some cases. For example, the
                YUI Compressor tool will issue a warning if it finds more than one var
                statement in a scope. JSLint's onevar option lets me avoid these warnings.


                regards,
                Stefan
              Your message has been successfully submitted and would be delivered to recipients shortly.