-2 points
*

WTF, I have never used nor seen “j.”

I don’t usually have to name these variables these days though. Pretty much everything I use has foreach or some functional programming type stuff.

And like that, the off-by-one mistakes disappear.

permalink
report
reply
3 points

It was very common in text books when showing nested loops

int nWhatTheCount = 0;
for (int i = 0; i < 10; i++) { 
    for (int j = 0; j < i; j++) { 
        for (int k = 0; k < j; k++) { 
            for (int l = 0; l < k; l++) { // and on, and on
                nWhatTheCount++;
            }
        }
    }
}
permalink
report
parent
reply
3 points

j is for a loop in a loop.

permalink
report
parent
reply
1 point

foreach is useful when you don’t need to know the index of something. If you do, conventional i, j, k, etc. are useful.

A lot of it depends what you’re doing (number crunching, for instance) or if you’re in a limited programming language (why won’t BASIC die already?) where parallel arrays are still a thing.

permalink
report
parent
reply
19 points

People who name iterators with one letter have no soul.

permalink
report
reply
9 points

two letters it is then

permalink
report
parent
reply
4 points

And people who iterate over 3D space using firstDimensionIndex, secondDimensionIndex, and thirdDimensionIndex instead of x, y, z have no sense 😜

permalink
report
parent
reply
2 points

x, y, and z are absolutely fine for spatial addressing.

permalink
report
parent
reply
30 points

A useful tip I picked up was to use ii instead of j for an inner loop. It’s far more distinct than j.

If for some terrible reason you have even more inner loops you can easily continue the trend i, ii, iii, iiii, iiiii - or iv, v if you’re feeling roman

permalink
report
reply
35 points

If you have the need to nest 5 levels of for-loops, I suggest taking a step back and rethinking your approach, my friend.

Even if that other approach is just refactoring it into separate methods.

permalink
report
parent
reply
3 points

I just do i2, i3, etc

permalink
report
parent
reply
1 point

Becomes unreadable if you’re using the iter values a lot

permalink
report
parent
reply
1 point

When you have multiple indices you’re also bound to have multiple cardinals those indices count up to, say foo.length and bar.length, so foo_i and bar_i are perfectly legible and self-documenting. A bit Hungarian but Hungarian is good in small amounts. Unless you’re dealing with width and height in which case it’s x and y but it’s not that width_i would be incomprehensible.

permalink
report
parent
reply
2 points

Two or three "i"s is readable, but any more and you’re counting.
I’Ve started using i, k, m, n that’s usually enough.

permalink
report
parent
reply
1 point

At this point we might as well go full Roman as you suggested. MXMCIIV to MXMCCVII as indices.

permalink
report
parent
reply
1 point

chuckles in Python

permalink
report
reply
1 point

I’m honestly prefer short but (usually) complete words. Somewhere along the line I realized that being explicit really helps when you need to change it later.

permalink
report
reply
5 points

due to convention everybody understands what i and j are, I don’t think they need longer names. If it’s something more complicated than a counter or index then maybe you should be using a foreach loop instead (if language supports it)

permalink
report
parent
reply
3 points

I generally use ‘count’ for a counter and ‘idx’ for index.

I’m not using C or Java languages though - if I were I would probably go with the more classic terse approach.

Also, if I’m reviewing a PR and I have to load more of the diff context to understand what a variable represents, then that variable has the wrong name.

permalink
report
parent
reply
3 points

Even as an embedded C developer I use “idx” and “count” instead of “i”. Not just because I’m a member of the “slightly longer but more descriptive names are better” gang, but also for searchability. If I’m trying to track down where an array is accessed in a loop, for example, “idx” is more likely to take me only to the results I’m looking for and not also the “i” in int8_t or whatever.

permalink
report
parent
reply

Programmer Humor

!programmerhumor@lemmy.ml

Create post

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.

Community stats

  • 3.6K

    Monthly active users

  • 1.5K

    Posts

  • 35K

    Comments