9

Please, stop using `i`, `j` and `k` in loops... specially if they are nested, help the fellow programmer after you

Comments
  • 24
  • 4
    Name your iterators and other variables in problem-domain, if possible.
  • 4
    Just 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.
  • 5
    don't know. they are pretty common. just get used to it
  • 14
    index, jndex, kndex
    /sndex
  • 3
    @electrineer in one of my scripts there are literally: i, ii and iii indexes in nested loops. 😁
  • 3
    indeed! Use i0, i1, i2, i3 et al.
  • 5
    @iiii

    Can I comment your code?

    while(i){

    // re

    while(ii){

    // ree

    while(iii){

    // reee

    }

    }

    }
  • 5
    @Demolishun so instead of i, j, k you should use r, re, ree, reee, reeee etc.
  • 10
    Yeah, i,j,k are too readable! I like to write mine like this:

    for(c...){
    for(u...){
    for(n...){
    for(t...){
    queryAGiantDatabaseOfc();
    }
    }
    }
    }
  • 3
    I jk always _0, _1, _2...
  • 3
    @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?
  • 7
    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.
  • 2
    @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.
  • 2
    @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.
  • 2
    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.
  • 2
    @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.
  • 4
    i,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.
  • 0
    @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?
  • 3
    @frogstair numbers in variable names are absolutely terrible and should be illegal
  • 0
    @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.
  • 1
    @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?
  • 1
    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.
  • 1
    @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.
  • 0
    @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
  • 1
    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.
  • 2
    No, i do t think I will.
  • 0
    @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.
  • 2
    @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. :)
Add Comment