Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad practice to use the same variable name in multiple for-loops?

I was just linting some JavaScript code using JSHint. In the code I have two for-loops both used like this:

for (var i = 0; i < somevalue; i++) { ... } 

So both for-loops use the var i for iteration.

Now JSHint shows me an error for the second for-loop: "'i' is already defined". I can't say that this isn't true (because it obviously is) but I always thought this wouldn't matter as the var i is only used in that specific place.

Is it bad practice to use for-loops this way? Should I use a different variable for each for-loop in my code like

//for-loop 1 for (var i = 0; ...; i++) { ... }  //for-loop 2 for (var j = 0; ...; j++) { ... } 

Or is this on e of the errors I can ignore (because it doesn't break my code, it still does what it is supposed to do)?

JSLint btw. stops validating at the first for loop because I don't define var i at the top of the function (that's why I switched to JSHint in the first place). So according to the example in this question: Should I use JSLint or JSHint JavaScript validation? – I should use for-loops like this anyway to confirm JSLint:

... var i; ... //for-loop 1 for (i = 0; ...; i++) { ... } ... //for-loop 2 for (i = 0; ...; i++) { ... } 

This also looks good to me, because this way I should avoid both errors in JSLint and JSHint. But what I am uncertain about is if I should use a different variable for each for-loop like this:

... var i, j; ... //for-loop 1 for (i = 0; ...; i++) { ... } //for-loop 2 for (j = 0; ...; j++) { ... } 

So is there a best practice for this or could I just go with any of the codes above, meaning I choose "my" best practice?

like image 220
TimG Avatar asked Apr 03 '13 09:04

TimG


People also ask

Can you use the same variable in multiple for loops?

@ Nikolay: Yes, it is, if that's where the declaration actually occurs regardless of where you place it.

Why is it a bad idea to reuse the same identifier in different nested scopes?

You may get compiler warnings if you create a variable with the same name as another variable in an outer scope. With good reason, because that is going to be confusing and error prone. Avoid it whether you get warnings or not.

Is using multiple loops bad?

Nested loops are frequently (but not always) bad practice, because they're frequently (but not always) overkill for what you're trying to do. In many cases, there's a much faster and less wasteful way to accomplish the goal you're trying to achieve.

Can we use same variable name in two functions?

So, yes, you can have variables of the same name in different scopes.


1 Answers

Since variable declarations are hoisted to the top of the scope in which they appear the interpreter will effectively interpret both versions in the same way. For that reason, JSHint and JSLint suggest moving the declarations out of the loop initialiser.

The following code...

for (var i = 0; i < 10; i++) {} for (var i = 5; i < 15; i++) {} 

... is effectively interpreted as this:

var i; for (i = 0; i < 10; i++) {} for (i = 5; i < 15; i++) {} 

Notice that there is really only one declaration of i, and multiple assignments to it - you can't really "redeclare" a variable in the same scope.

To actually answer your question...

is there a best practice for this or could I just go with any of the codes above?

There are varying opinions on how best to handle this. Personally, I agree with JSLint and think the code is clearer when you declare all variables together at the top of each scope. Since that's how the code will be interpreted, why not write code that looks as it behaves?

But, as you've observed, the code will work regardless of the approach taken so it's a style/convention choice, and you can use whichever form you feel most comfortable with.

like image 59
James Allardice Avatar answered Sep 17 '22 20:09

James Allardice