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

Re: "Too many var statements"

Expand Messages
  • 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 1 of 6 , Aug 27, 2009
    • 0 Attachment
      --- 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 2 of 6 , Aug 27, 2009
      • 0 Attachment
        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.