this post was submitted on 09 Jul 2023
336 points (100.0% liked)

Programmer Humor

854 readers
1 users here now

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

Rules:

founded 5 years ago
MODERATORS
 
top 33 comments
sorted by: hot top controversial new old
[–] Poob@lemmy.ca 30 points 1 year ago* (last edited 1 year ago) (3 children)

i is for index. j is simply the next letter and we're too lazy to think up something meaningful

[–] PM_ME_VINTAGE_30S@lemmy.sdf.org 9 points 1 year ago (1 children)
[–] Gork 2 points 1 year ago (1 children)
[–] CoffeeDev@lemmy.studio 1 points 1 year ago* (last edited 1 year ago)
[–] DraughtGlobe@feddit.nl 6 points 1 year ago

I always thought it stood for iterator

[–] chahk 2 points 1 year ago (1 children)
[–] StudioLE@programming.dev 18 points 1 year ago (5 children)

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

[–] hstde@lemmy.fmhy.ml 17 points 1 year ago

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.

[–] biscuit@lemdro.id 2 points 1 year ago

I just do i2, i3, etc

[–] barsoap@lemm.ee 1 points 1 year ago

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.

[–] exu@feditown.com 1 points 1 year ago

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.

[–] Gork 1 points 1 year ago

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

[–] barsoap@lemm.ee 14 points 1 year ago* (last edited 1 year ago)

It depends. x and y are either elements or coordinates, a and b usually elements though in e.g. Haskell reserved (by convention) for type variables.

The i j k l series is reserved for indices. n m etc. are the counts of something, as such you'll see i counting up to n. Both are due to mathematical sum notation and general mathematical convention. Random google result:

Let x~1~, x~2~, x~3~, …x~n~ denote a set of n numbers. x~1~ is the first number in the set. x~i~ represents the ith number in the set.

...if you're using a language in which you use i often chances are you should stop coding in C and get yourself a language with iterators. Manual loops are a bug magnet.

[–] Dirk@lemmy.ml 12 points 1 year ago (2 children)

People who name iterators with one letter have no soul.

[–] lowleveldata@programming.dev 5 points 1 year ago

two letters it is then

[–] catfish@lemmy.ml 1 points 1 year ago (1 children)

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

[–] Dirk@lemmy.ml 1 points 1 year ago

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

[–] Luvon 11 points 1 year ago* (last edited 1 year ago)

I generally use a for each type loop or a map because I am usually applying some function across a collection, and in both cases I use the singular name from the collections plural.

’Cities.map(city -> …)’

For (val city in cities)

If I actually need the index for some reason I still prefer loop structures that give me the index and the item together

*note syntax pulled out of my head and not necessarily belonging to any specific language.

For ( city, index in cities)

cities.map((city, index) -> … )

If I need to double loop a matrix array I would use rowIndex and ColIndex for the indexes.

[–] Spzi@lemm.ee 10 points 1 year ago

I find it hard to read when these are together:

  • i, j, l
  • n, m, u, v, w

From all the possible character combinations, somehow the lookalike combinations are among the most popular. Yes, probably comes from math. I hated it even more when my math prof's i and j on the board were indistinguishable.

[–] Cowabunghole@lemmy.ml 8 points 1 year ago

It's my understanding that i,j are conventionally used in mathematics which carried over into programming, but specifically it comes from Fortran in which all integer variables start with "I" through "N" based on said mathematical convention

[–] Dohnakun@lemmy.fmhy.ml 6 points 1 year ago* (last edited 1 year ago)

for <thing> in <amount> do...

[–] basuramannen@discuss.tchncs.de 4 points 1 year ago (1 children)

I don't like i and j since they are commonly used for imaginary numbers. I like to start on n. Probably because I do DSP.

load more comments (1 replies)
[–] bilb@lem.monster 3 points 1 year ago* (last edited 1 year ago) (1 children)

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.

[–] karbonkel 3 points 1 year ago

j is for a loop in a loop.

[–] Lewistrick@feddit.nl 2 points 1 year ago

chuckles in Python

[–] librecat@lemmy.basedcount.com 1 points 1 year ago

don't mind i but personally always use index or x, y, z for games

[–] Zucca@sopuli.xyz 1 points 1 year ago

Well. I guess I'm then a some kind of heretic then. 🤷

[–] menturi@lemmy.ml 1 points 1 year ago

I started using the first letter of the thing I am iterating over. This is particularly helpful with nested loops so I can easily remember which index variable corresponds to which thing.

[–] roi@lemmy.blahaj.zone 1 points 1 year ago

When my brain doesn’t work I’ll resort to naming them the single of the plural. Like keys turns into key when i don’t wanna call it “objkey” or “outrageouslylongnamethatmayormaynotbeafittongwordtodescribeakey”

load more comments
view more: next ›