Poob ( @Poob@lemmy.ca ) 30•1 year agoi is for index. j is simply the next letter and we’re too lazy to think up something meaningful
jndex
Gork ( @Gork@beehaw.org ) 2•1 year agokndex
CoffeeDev ( @CoffeeDev@lemmy.studio ) 1•1 year agolndex
DraughtGlobe ( @DraughtGlobe@feddit.nl ) 6•1 year agoI always thought it stood for iterator
Chahk ( @chahk@beehaw.org ) 2•1 year agoi2?
piotrek416 ( @piotrek416@szmer.info ) 3•1 year agoii
StudioLE ( @StudioLE@programming.dev ) 18•1 year agoA useful tip I picked up was to use
ii
instead ofj
for an inner loop. It’s far more distinct thanj
.If for some terrible reason you have even more inner loops you can easily continue the trend
i
,ii
,iii
,iiii
,iiiii
- oriv
,v
if you’re feeling roman hstde ( @hstde@lemmy.fmhy.ml ) 17•1 year agoIf 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 ( @biscuit@lemdro.id ) 2•1 year agoI just do i2, i3, etc
exu ( @exu@feditown.com ) 1•1 year agoTwo 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 ( @Gork@beehaw.org ) 1•1 year agoAt this point we might as well go full Roman as you suggested. MXMCIIV to MXMCCVII as indices.
barsoap ( @barsoap@lemm.ee ) 1•11 months agoWhen you have multiple indices you’re also bound to have multiple cardinals those indices count up to, say
foo.length
andbar.length
, sofoo_i
andbar_i
are perfectly legible and self-documenting. A bit Hungarian but Hungarian is good in small amounts. Unless you’re dealing withwidth
andheight
in which case it’sx
andy
but it’s not thatwidth_i
would be incomprehensible.
barsoap ( @barsoap@lemm.ee ) 14•1 year agoIt depends.
x
andy
are either elements or coordinates,a
andb
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 seei
counting up ton
. Both are due to mathematical sum notation and general mathematical convention. Random google result:Let x1, x2, x3, …xn denote a set of n numbers. x1 is the first number in the set. xi 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. dark_stang ( @dark_stang@beehaw.org ) 14•1 year agox is used for map, filter, etc. a and b are used for sorts, comparisons and merges. y might be used if I’m doing multiple lambda expressions (but that means I’m in a bad place already). I have no idea why, but these are firm rules in my brain.
DogMuffins ( @DogMuffins@discuss.tchncs.de ) 8•1 year agoI’ve gotten used to using the singular form as in…
records.filter((record) => …)
Not saying this way is better but it works for me.
karbonkel ( @karbonkel@beehaw.org ) 2•1 year agoI do this too. I hate using just
x
, because it’s so non-descriptive.
russ ( @russmatney@programming.dev ) 1•1 year agoYes! I love using x (and xs) for functions over whatever the thing is (or things are).
𝘋𝘪𝘳𝘬 ( @Dirk@lemmy.ml ) 12•1 year agoPeople who name iterators with one letter have no soul.
lowleveldata ( @lowleveldata@programming.dev ) 5•1 year agotwo letters it is then
catfish ( @catfish@lemmy.ml ) 1•1 year agoAnd people who iterate over 3D space using firstDimensionIndex, secondDimensionIndex, and thirdDimensionIndex instead of x, y, z have no sense 😜
𝘋𝘪𝘳𝘬 ( @Dirk@lemmy.ml ) 1•1 year agox, y, and z are absolutely fine for spatial addressing.
Luvon ( @Luvon@beehaw.org ) 11•1 year agoI 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 ( @Spzi@lemm.ee ) English10•1 year agoI 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 ( @Cowabunghole@lemmy.ml ) 8•1 year agoIt’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
for <thing> in <amount> do...
basuramannen ( @basuramannen@discuss.tchncs.de ) 4•1 year agoI 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.
Bilb! ( @bilb@lem.monster ) 3•1 year agoWTF, 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 ( @karbonkel@beehaw.org ) 3•1 year agoj
is for a loop in a loop.
Lewistrick ( @Lewistrick@feddit.nl ) 2•1 year agochuckles in Python
Aiden ( @roi@lemmy.blahaj.zone ) 1•1 year agoWhen 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”
Zucca ( @Zucca@sopuli.xyz ) English1•1 year agoWell. I guess I’m then a some kind of heretic then. 🤷
menturi ( @menturi@lemmy.ml ) English1•1 year agoI 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.