3

Something is wrong with my code or with plovr. I went to JSLint to get some help. However, JSLint seems to consider this as a fatal error and refuses to check more of the code:

for (var i = 0; i < data.length; i += 4) {

Why? I like this way of declaring "I".

JSHint does not seem to be an alternative if you are on Windows. I am trying The Online Lint at the moment. I have a feeling that this bug will be a bit difficult to find so I would be glad for suggestions about good code checkers.

Leo
  • 4,136
  • 6
  • 48
  • 72
  • 3
    My personal opinion is that JSLint is retarded. But opinions may vary on that sensitive topic. – Denys Séguret Apr 16 '14 at 09:19
  • 2
    JSLint is not the gospel regarding how to code JS. Rather, it merely enforces someone's opinion of what constitutes good code. There is nothing wrong with your code. Like the previous commenter, I find JSLint far more annoying and anal than it is helpful. – Mitya Apr 16 '14 at 09:20
  • @Touchpad Please read more about variables in JavaScript. Moving the `var` declaration to the top of the function doesn't change anything. – Denys Séguret Apr 16 '14 at 09:20
  • possible duplicate of [JSLint error: Move all 'var' declarations to the top of the function](http://stackoverflow.com/questions/4646455/jslint-error-move-all-var-declarations-to-the-top-of-the-function) – Chris Apr 16 '14 at 09:22
  • 1
    @dystroy JSLint has rules that protect people from shooting themselves in the foot with things that have weird behavior which is not generally very well understood -- If you're one of the guys who perfectly understands that weird behavior perfectly and you work only with people who understand it just as well -- then yeah JSLint is of no use to you, otherwise it may be of use. – Mihai Stancu Apr 16 '14 at 09:22
  • @MihaiStancu I specifically disagree regarding this rule. Having the increment variable declared in the `for` statement **is** a good practice I would recommend. Of course it should be moved if the increment variable is used elsewhere in the function. – Denys Séguret Apr 16 '14 at 09:24
  • 1
    @dystroy I can relate to the opinion but D. Crockford (of ECMA and JSLint) added it because the behavior of JavaScript is exactly this -- so in a sense by doing what the language already does you're not leaving any room for interpretation. – Mihai Stancu Apr 16 '14 at 09:27
  • What I mean by JavaScript behavior is that it breaks any `var x = 3` into 2 pieces: `var x` is put at the beginning of the function and `x = 3` is left where it was found (meaning that `x` is `undefined` before that). – Mihai Stancu Apr 16 '14 at 09:28
  • Some people might wrongly interpret code as if having block scoping or they might assume that using `x` before the `var x = 3` statement means using some other global `x` variable -- neither of which is true. – Mihai Stancu Apr 16 '14 at 09:31
  • @dystroy so what you're saying is generally good practice but in the case of JavaScript it only helps organize related stuff in the same visual region it doesn't respect the real syntactic meaning that JavaScript gives to it. – Mihai Stancu Apr 16 '14 at 09:33
  • So the code checker and ECMA is out of sync. If they want the declarations at the beginning of the scope they better change the specs of ECMA Script. This is a mess. ;-) – Leo Apr 16 '14 at 09:38
  • @Leo -- it's one of those "it's not a bug, it's a feature" things, ECMA offers the syntactic freedom while still doing what it wants behind the scenes. – Mihai Stancu Apr 16 '14 at 09:44
  • @MihaiStancu JavaScript hoists the variable declarations so that you're allowed to write a clean code by putting the variable declarations where they're logically used. It's a feature. When there's syntactic sugar in a language, don't fight it, take advantage of it ! – Denys Séguret Apr 16 '14 at 09:48
  • @dystroy Syntactic sugar isn't always the best thing since sliced bread. Sometimes (like this one) the sugar makes it easy to make mistakes. Again if you're aware of (all of these) hidden behaviors and all of your team mates are as well... then feel free to use the style of coding you like. If you fear shooting yourself in the foot might be an issue then find a tool that helps you at your exact level. – Mihai Stancu Apr 16 '14 at 09:58

3 Answers3

6

I agree with Niet the Dark Absol that the tool is opinion-based. Reflect on all the rules it imposes and whether they actually make sense in your specific project or not. It's also not the only "lint" tool for JavaScript. Maybe ESLint or another such tool is more suited to your needs.

But I also disagree with him: It is not good practice to declare all your variables at the start of your function, especially not if the function is rather long, since this makes comprehension of your program much harder. IMHO this holds regardless of how the scoping of JavaScript works: It's not about program semantics, it's about code readability!

I would argue that a variable should be declared as close to its first use as possible (i.e. in your case: in the for loop). This ensures that someone reading your code (a colleague or yourself three months from now) has too keep as little information in their head as possible. Declaring all your variables at the start forces the reader to keep all those variables in mind throughout the entire function. Questions like "what's the current value of this variable?" or "what is its purpose?" become harder to answer.

Furthermore, you are much more tempted to reuse a variable for more than one purpose. This is not only confusing, but also dangerous! Values might "leak" from the first use to the second. This can lead to subtle, hard-to-debug problems.

Fabian Streitel
  • 2,702
  • 26
  • 36
4

My personal opinion is that JSLint is retarded. But opinions may vary on that sensitive topic.
— dystroy, 45 secs ago

Hey, that's actually a valid point. JSLint is entirely constructed on opinion, and it not the word of god.

However, general good practice is to declare all your variables in one place, at the start of the function block they are in:

function doSomething() {
    var a, b, c, d;
    a = 1;
    c = 10;
    for( b = a; b < c; b++) {
        d = b*2 + a-c;
        alert(d);
    }
}
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
-1

Because creating a bar in the for loop creates a global var. it's one of those things that JavaScript does and most people don't realize. And if you don't and you or someone else creates that same var in the scope of that function or global scope then that my friend would be one of the famous JavaScript foot guns.

andre mcgruder
  • 1,120
  • 1
  • 9
  • 12