Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
iiii92264yJust don't use indexes if possible. Most of the times indexes are redundant because all that's needed is an iteration over everything in a collection.
-
iiii92264y@electrineer in one of my scripts there are literally: i, ii and iii indexes in nested loops. 😁
-
-
Hazarth94764yYeah, i,j,k are too readable! I like to write mine like this:
for(c...){
for(u...){
for(n...){
for(t...){
queryAGiantDatabaseOfc();
}
}
}
} -
@Hazarth I assume QueryAGiantDatabaseOfcourse will open a new connection to the database, authenticate, make a perfectly identical query each time involving several joins on unindexed columns and then skip ahead in the results?
-
I disagree with just about everyone in this thread. If you're having problems reading `i` in a for loop, that's on you.
The real problem you're describing is too many nested loops or too complicated of code all put together. -
@junon The isolated case of having an "i" in a regular for loop isn't the problem. The problem is that very often the loop variable isn't an integer counting from zero to somewhere one by one, and braindead devs still just call it "i" because it stands for iterator, despite the fact that there would be something to document and the sole purpose of variable names is to serve as documentation.
-
@homo-lorens But that's not how it was qualified in the OP. If the OP had said that, there'd be something to agree with there.
-
I have had issues telling the difference between and i and j. Some languages bleed variables outside of (ex: for(var i in something)). So it could cause issues if always using the same thing. I could see if being confusing in large functions. But as someone said, maybe those should be smaller.
-
@junon Using j for the inner loop is arguably a bad idea anyway because they are visually similar. And single nested loops aren't unheard of even in very clean code.
-
arekxv10544yi,j,k is perfectly fine and best option actually if the loop body has up to 5 lines of code max. Although if you have for-each support and the algorithm can use that, always use that.
-
@arekxv If you have all strings of the entire English alphabet at your disposal and the outer loop uses i, is it really fine for the inner loop to use the single most similar string possible? I get that it's conventional, but is it good?
-
@frogstair numbers in variable names are absolutely terrible and should be illegal
-
@homo-lorens "if necessary", i would argue.
many times the loops are just list iterators, and having the control var named "index" or whatnot makes all the statements using it annoyingly long and much less readable. -
@homo-lorens 'The problem is that very often the loop variable isn't an integer counting from zero to somewhere one by one' now I'm curious - do you really run into that problem OFTEN? Got some examples?
-
A single loop with i is fine, but nested loops are a bigger issue.
Looping over a plain 2D array with i and j is so classic it's not an issue.
But if you have complex objects, and are using the index-variables for something more than `let item = arr[i]` then you might wanna consider naming them after WHAT they are indexing.
Something like is not optimal code:
console.log("Employee", j, "works for Company", i);
You might benefit from calling your variables companyIndex and employeeIndex if you're doing multiple things with the index-variables. -
arekxv10544y@homo-lorens Most of the time developers know and expect j in a nested loop because its so prevalent and you would actually slow their code read speed down if you use a different letter as it would make them stop to slow down and look at the simple algorithm way more closely until they understand that its just a different interator variable name. Its something we are stuck with unfortunately.
There are places like computer graphics where pixel drawing algorithms will benefit more from using x and y instead of i and j, so its not like you would need to always use it. It depends on what you are doing. -
@jiraTicket Well, if I'm iterating over an iterable datastructure I usually use foreach. since every language except maybe C (even C++ now) has it, so the only times I'm left with for loops are when
- I'm iterating over a subsection of a list-like datastructure. In this case the iterator should somehow represent why that subsection is special
- I'm just repeating something x times. In this case I always call the iterator _, which is the cross-language standard name for unused
- I'm iterating over a multi-dimensional array, or an array of arrays (consider all students in all classes). In this case I name the index in such a way as to reflect the collection it iterates through.
- The particular algorithm requires selecting every nth index (error detection codes do that for example), in this case it probably also deserves a comment, but if not then the variable should at least have a name like even_index or i_third -
I didn't actually find a reason right now to go through a list backwards, but I remember doing it a few times and I'm not one to obfuscate code without purpose so there are probably good reasons.
-
@arekxv My code read speed slows down by a factor of 4 and I have to increase the fontsize whenever I find two variables that only differ in the character pairs m-n, m-w, i-j or l-i. I guess it's a matter of preference but I dislike similar names enough to switch to a and b when the need for two loops occurs.
-
arekxv10544y@homo-lorens I know what you are getting at. They are too similar. But once you see them long enough it actually becomes weird when someone doesn't use them so you start wondering if you are missing something. In the end as long as code is cleanly written I am ok for any iteration letters. :)
Related Rants
Please, stop using `i`, `j` and `k` in loops... specially if they are nested, help the fellow programmer after you
rant
code review
loops
js
empathy
code