8
ZioCain
5y

Should I be ashamed?
I hardly ever use "i" as a counter in for loops
I most likely use "k" and "j"; "i" stands for "index"

Comments
  • 2
    Nah, I often use k,v,n in for loops.
  • 7
    Well.. aren't loops about interating over the indices?

    And yes you should. The only right way is using "cI", "theX" or "n" as index.
  • 8
    i = index / iterator
    So why wouldn't you use it?
    Depending on what your iterating over it makes perfect sense usually.
  • 0
    Yes, you should.
  • 3
    unless it's a traversal of spatial coordinates/array that represents space, where i use axis names as control vars (x, y, z), or something more complex when there are valid descriptive names for each index, i always use
    "c" for a single loop, and "c1, c2,..." for nested ones.

    derived from "cyklus", which is my native language's word for loop (and kinda works in english too, since 'cycle' is a word).

    OR, if it's a loop used to do interpolation over time in unity coroutines, i do t, except those are while(t < 1) loops with increment happening in body as "t += Time. deltaTime;"

    "i, j, k" always seemed like a vestige crap from ye olden days where single-letter names when people thought it somehow saves on memory or something, or when most programmers were mathematicians who are forbidden from havin useful variable names in formulas.

    i also don't remember when's the last time (except the loop's "c", interpolation loop's "t" , and x/y/z coords) i used ANY single-letter variable.
  • 1
    I try to use "row" and "col" for loops on a matrix. Personally, I think I,j are terrible - they look too similar and they have zero context most of the time. If it's a single loop, I use idx.
  • 12
    Laughs in functional programming.
  • 1
    @eeee arr.map((whatDoesThisDo, i) => arr[i])
  • 1
    @jdebs that's often available in functional languages, but often unnecessary. I use Kotlin (multi paradigm) and I usually use

    @highlight

    collection.forEach { println(it) }
    // Instead of
    collection.forEachIndexed { index, item -> /* ...*/ }
  • 0
  • 0
    @eeee haha yeah, I was just being silly by using functional paradigms to access elements imperatively. I've had too much coffee this morning, just ignore me.
  • 1
    With the thinking of "somebody else has to maintain this" using a naming convention that you like and have to explain why you like it doesn't seem like the best approach to me. Using I, j, k... Is expected as convention and results in the same functionality.

    If the index makes sense to the data being iterated over then a better name can be used but its usually independent.
  • 0
    I tap "for" then <tab>n and use whaever variable name IDE generates
  • 0
    What do k and j stand for in your code? What are tgey abreviations of?
  • 0
    @NoToJavascript you obviously don't use vim then. What would you choose if the ide forced you to choose.
  • 0
    @netikras shouldnt j and k only be used within a nested for loop? So, they should be either related to either the element in the original list that's being iterated or defined in a parent loop. It's always going to be an element of a list anyway and the name of the list should be descriptive enough to tell you what the elements are.
  • 0
    @cmarshall10450 name it whatever you want. Just make sure it's obviously clear what the name means and why the name has been chosen.
  • 0
    @cmarshall10450 probably i,j, ii,jj
  • 0
    No it's totally normal. It depends on you eventually.
  • 2
    I use 'i' as index only when my for loop is complex...

    P.S.: This was my attempt at a joke
  • 0
    @Chrupiter Also works for imaginary loops
  • 1
    I rarely use loops without named vars, but when I do, I use `i` or `a` or `c`.

    `i` because tradition
    `a` because the loop var is the "accumulator" (CS habit)
    `c` out of assembly habit (cx)
  • 1
    Loop I,v for index and value
    Loop inside of the loop o,p because why not
    Loop inside of that loop u,y
Add Comment