1.5k
u/Kooneybert Jun 06 '20
The iteration variable makes sense to be called i. j is just the next number in alphabet.
375
u/RedMantisValerian Jun 06 '20
i stands for iteration, j stands for jiteration
198
49
29
6
u/baseball2020 Jun 06 '20
Wait what we’re iterating?!?! Ok do it now! Just in time iteration - jiteration
→ More replies (3)5
Jun 06 '20
After a couple of coffees debugging that shit code (that I wrote) late into the night, it's a jitteration alright.
69
u/frosted-mini-yeets Jun 06 '20
None of you have ever had nested loops and gone i, j, k, l?
57
u/MattTheGr8 Jun 06 '20
I do a weird thing and skip L and go right to M, to avoid confusion between lowercase L and numeral 1, which look identical in some monospaced fonts.
→ More replies (7)135
34
Jun 06 '20
Once you reached the end of the alphabet you may be in some deep trouble.
That's some serious O(n4 ) shit you got going there, kiddo. Better hope n is small any you never need to debug that mofo.
13
→ More replies (3)9
u/AndrasKrigare Jun 06 '20
I just make every line of my code in a loop that runs once. That way if I every need to make a loop, the structure is all there.
→ More replies (1)→ More replies (16)6
Jun 06 '20
I try and find some sane representation in that case. Even if it's still a letter I'd use the first letter of what it is if that makes sense. Makes it easier to follow in the future.
Eg if you had book, page, word, letter I might use b, p, w, l.
18
u/B4-711 Jun 06 '20
why not just use book, page, word, letter? makes for some very readable code.
→ More replies (4)13
Jun 06 '20
i = iteration
j = just the next letter
k = keep going
l = last one14
Jun 06 '20
m = maybe one more?
n = no more please
o = oh no
p = please, I beg you to stop
q = qq→ More replies (2)12
Jun 06 '20
I didn't know it meant iteration. I use i for "item"
→ More replies (7)16
u/down_vote_magnet Jun 06 '20
It means “index” because iteration 0 makes no sense, but index 0 does.
→ More replies (1)29
u/Julio974 Jun 06 '20
i, j, k, l, then I start to consider why the fuck I have so many loops
→ More replies (1)13
u/Axe-actly Jun 06 '20
It's not too nested if you don't have to go back to "a" after doing the whole alphabet.
→ More replies (1)6
u/leo60228 Jun 06 '20
this isn't actually why! the trend originated in fortran, which defined variable types based on the first character in their name. i and j were used for integers, making them the two shortest possible names for integer variables.
→ More replies (1)5
Jun 06 '20
The i and j constants come from FORTRAN. Absent an IMPLICIT statement, undeclared variables and arguments beginning with i through n (the "in" group) will be integer, and all other undeclared variables and arguments will be real.
Engineers used to learn FORTRAN for their engineering work. Computer engineers used to study it as part of the engineering classes. They’d get used to naming ints with i and j in FORTRAN and follow that pattern when moving to other languages.
7
u/microbit262 Jun 06 '20
And if you have to make a loop around the i-loop that was not planned before or is inserted later it obviously becomes h, because no one changes the original.
6
u/DaemonActual Jun 06 '20
*Me nesting the OG for loop and renaming the i (and every loop after)*
What heresy is this?!
6
3
u/Uhfuecu Jun 06 '20
It’s a notation borrowed from math. In a math series the iteration index is represented with i, if there is a need for more indexes you go i-j-k, and eventually greek letters.
3
→ More replies (84)2
130
Jun 06 '20
You know the rules and so do I
50
108
u/creskentydoll Jun 06 '20
In physics calculus dimensions are represented as i, j, and k. I don't know if there's a clear connection but technically i is used to iterate over a 1D surface.
83
u/plaisthos Jun 06 '20
Yes math in general uses i,j,k in sums long before computers were invented
9
u/tralltonetroll Jun 06 '20 edited Jun 06 '20
Integer indices i,j,k are something else than basis vectors i, j, k. (Or overarrow notation, as physicists use, as their definition of vector is quite restrictive.)
→ More replies (1)8
u/plaisthos Jun 06 '20
Yes. And we often used u,v,w for base vectors in linear algebra but that is all nitpicking when the bigger picture is that all these usages predate CS by decades or centuries
→ More replies (5)22
190
Jun 06 '20
I always used "i" as "index", "j" as "jndex", "k" as "kndex"...
24
6
3
3
u/golgol12 Jun 07 '20
Let me show you a better way.
Name your indexes <what index references>Index. You have a bacon[]? You use bacon[ baconIndex ]; You have a list eggs;? You use eggIter;
That way, later on, when you need to mix some egg and bacon together, you just do bacon[ baconIndex ] + eggIter; See how easy that makes things?
2
u/Mya__ Jun 06 '20
I've never been comforatable with i as it's always a vector ( î ) in my head. I usually name the control specifically if it's an independant control or use the array index values/size in relation to the iteration.
is this bad for processing efficiency? I'm not a pro at all. It's easier in my head to make everything relative to each other tho when iterating to make boundaries remain flexible to changing sizes of multidimensional arrays.
413
u/tedshif Jun 06 '20
The story I heard was that Fortran variable names were limited to a single letter, and each letter had a pre-defined type. The letter i was the first in the group of integers, so when people needed a simple variable to increment in a DO loop (Fortran’s for loop) they used i. The letter i standing for “increment” also probably raised its popularity, along with other things. I have no way to verify this, but it’s a neat story, so I thought I’d share it.
95
u/Sjsamdrake Jun 06 '20
Early FORTRAN variable names weren't just one letter, but the first letter of the name determined the default type. Variables starting with I through N were integers.
51
u/TrumpLyftAlles Jun 06 '20
This is the correct answer.
Source: FORTRAN was my first programming language, 1969
24
15
→ More replies (3)8
→ More replies (7)3
u/r3jjs Jun 06 '20
When they added the ability to declare variables lead to the phrse:
"GOD is real, unless declared as an integer."
35
u/radome9 Jun 06 '20
Fortran variable names were limited to a single letter,
How our species ever made it down from the trees is beyond me.
→ More replies (3)18
Jun 06 '20 edited Jun 06 '20
Has more to do with algebra though.
By convention, discrete integers are named after their initial, “i” , which also is the first letter of “item”. Further variables are simply named by taking the next one.
→ More replies (1)15
Jun 06 '20
I kind of like the idea of using i, ii, iii, iv, v, vi... but I feel moving from convention in that manner would annoy my colleagues.
11
u/kloga12 Jun 06 '20
I've seen i and ii sometimes, but going full roman numbers is something I've never seen. Could be cool visually and makes the level of recursion more clear, but you have to type more characters.
4
u/SpaceMathStuff Jun 06 '20
I love the many different takes on what word "i" is the first letter of! Personally I always saw it as standing for "iteration"
3
u/cec772 Jun 06 '20
I’m old enough to have been a FORTRAN teaching assistant in college, and thats what I remember. (But admittedly I my memory is a bit blurry from those days)
2
u/Pbx123456 Jun 06 '20
In FORTRAN integer variables started with I,j,k,,,. A friend that worked at IBM a very long time ago was told by a starting programmer that she had used up all the integer variables.
2
u/SpellSound Jun 06 '20
This is the correct answer. FORTRAN, one the first formal high level languages let you create implicit variables, with "i" being the start of the integers. It's a tradition that has carried on to this day.
I think a computing history class (hitting topics like this) should be required for a CS degree. Knowing where it all comes from and how we ended up here can help you understand the "why's" a lot better. It also helps build an understanding and appreciation that we're all standing on the shoulders of giants and their influence is still felt today.
2
u/YetAnotherGuy2 Jun 06 '20
I'm shocked how far down this is and how few know this. What do kids learn these days?
FORTRAN I I which appeared in 1958 used fixed names. Integers had to be I, J, K, L, M & N which is why all example code from that era used I as counter.
It became the dominant language in science computing and as most learned programming at the University back in the days (no one had a personal computer until the late 70s after all) you learned it the science way back then. FORTRAN became the "mother tongue of scientific computing", so naturally when these folks went out into corporate, the room those habits with them
→ More replies (29)2
u/golgol12 Jun 07 '20
Yep, that's what I heard. However, Fortran can support more than a single letter variable names.
356
Jun 06 '20
Programmers: name of variable should be self explaining what variable is for
Also programmers: use i,j,x,y,z variables.
179
u/Caenir Jun 06 '20
I and j are so commonly used they are self explaining
→ More replies (1)129
u/evil_cryptarch Jun 06 '20
As are x, y, and z if you're working with cartesian coordinates.
→ More replies (20)5
u/highfire666 Jun 06 '20
And T when you're working with temperature or period, but never both. In that case you use temp! Unless you already used temp for temporary variables
→ More replies (5)38
u/Bobby_Bonsaimind Jun 06 '20
If you use x/y/z for loop variables and they have nothing to do with dimensions, I'm going to figure out where you live and I will nail a dead fish to your doorstep.
2
→ More replies (51)2
u/shinitakunai Jun 06 '20
For k,v in dict.items() is easy to understand as dicts have key and values.
Those are the cases where I use 1letter variables.
54
u/Butternubicus Jun 06 '20
I end up always using i as an integer iterator and x as an object iterator (eg. List.Select(x=> x...))
I have no idea where I picked it up, but by god it’s the law.
12
12
u/I_Was_Fox Jun 06 '20
When I do object iteration, I always do the first letter of the object: people.foreach(p => p...)
→ More replies (7)4
Jun 06 '20
I use
itfor objects (and pointers) - it's an abbreviation for iterator and since an object is referred to as "it" in English, it's pretty damn obvious.In expression-lambdas I use
_as the name if there is only one argument.→ More replies (5)→ More replies (15)2
45
u/qwasd0r Jun 06 '20
row, col, idx... I'm lawless.
15
u/cdreid Jun 06 '20
We know who you are, we have you on a list. We are coming for you anarchist
→ More replies (2)4
3
u/ogtfo Jun 06 '20
Row is not a proper replacement for i. Row implies its storing a row, whereas i implies the index of a row.
Using row for an index is just confusing at best. I'd give row_index or row_number a pass, but row is just a bad choice.
→ More replies (1)
18
u/jensroda Jun 06 '20
Something I find fascinating about programming is that our modern "language" for interacting with computers evolved very quickly, very recently. All these "ancient, forgotten" practices weren't created by ancient people in pre-recorded times, most of the people who standardized these practices are still alive and programming.
Imagine if the English language was written by one person, and you could still ask that person why they chose "the" to be the most common word, or why they implemented so many words from other languages that were already defined to begin with?
Anyway, I'm sure people use "i" so much simply because someone used it like that first, for completely arbitrary reasons, and everyone else did it too because bandwagon.
3
u/RCM94 Jun 07 '20
Except in this example there were ancient people because i,j,k etc were used for summations much before programming was a thing and the concepts are very similar.
→ More replies (1)
17
u/bonbonlable Jun 06 '20
I sometimes use n and k but mostly in Matlab only 😅
3
→ More replies (1)3
u/raging_ragdoll Jun 06 '20
That's beacause if you use i you can't use imaginary numbers, or that's what i've been told
→ More replies (3)3
u/InternalDot Jun 06 '20
You can use j as an imaginary number too, so it’s still doable
→ More replies (1)
17
Jun 06 '20
*laughs in functionals*
3
u/mktiti Jun 06 '20
While eliminating this problem, haskell is still guilty as fuck of using cryptic single letter variables.
Pointfree is cool tho.
→ More replies (1)
13
25
u/li-si Jun 06 '20
Homage to Dijkstra.
2
u/theProfessorr Jun 06 '20
Had a professor that joked about naming his kids i, j, k. Said he loved the name Dijkstra for the same reason.
10
11
u/Noch_ein_Kamel Jun 06 '20
iterator
jitterator
kidzbopirator
2
u/ogtfo Jun 06 '20
Kidzpobirator is a fine metaphor for what your code probably looks like when you're three loops deep.
8
u/mcnut77 Jun 06 '20
Us faux matlab programmers use ii and jj, unless we want to redefine the definition of an imaginary number.
34
Jun 06 '20
Because i is the index and j is the next letter in the alphabet.
→ More replies (5)37
Jun 06 '20
I would say i = iterator. It's only an index if you're accessing an array.
→ More replies (14)
5
11
u/Michel3951 Jun 06 '20 edited Jun 06 '20
x, y anyone? No?
edit: lowercase x,y
14
u/highfire666 Jun 06 '20
In a nested loop that has nothing to do with cartesian coordinates? You monster!
4
4
→ More replies (2)2
Jun 06 '20
Yep x, y, z are my first picks too. It's the way my teacher did it, I liked it, so I keep doing it. The ijk crowd can sue me, xyz will endure!
4
4
4
u/almarcTheSun Jun 06 '20
What's the show? This looks so familiar.
4
u/Nielsly Jun 06 '20
The Suite Life of Zack & Cody, that’s mister Mosby in the photo :)
→ More replies (2)
1
u/mightybanned Jun 06 '20
I prefer using camelCase and PascalCase in naming variables. By that way, I cant easily identify them when debugging, it also helps the other co-programmers to read the code easily.
1
3
3
Jun 06 '20
We get a lot of this from FORTRAN. FORTRAN got it from mathematics. Here’s a nice write up about it.
https://stackoverflow.com/questions/4137785/why-are-variables-i-and-j-used-for-counters
5
u/kennypu Jun 06 '20
I like to drive people crazy by using i, ii, iii and so on. I have also used i2
4
Jun 06 '20
I feel like i1 i2 i3 etc might be somewhat clarifying compared to i j k in iterative loops. The number is the loop layer.
→ More replies (1)
4
4
u/yharnamite_blood Jun 06 '20
Once I tried to use h,k as variables, The FBI was at my doorstep, TRUE STORY.🧐
→ More replies (2)
2
u/Ace-O-Matic Jun 06 '20
Because it's a clear way to signal to anyone reading the code: the variable's purpose as iteration value and its depth.
2
2
u/SpinatMixxer Jun 06 '20
When I started programming I knew about the i but not the j. When I encountered my first loop in a loop, I was thinking about how to name the new variable.
I did go for i, ii, iii, iiii and so on. Until my collegue saw it and explained me why that was not the way how we gonna do it.
2
Jun 06 '20
sigh, that's stupid. It's obviously better to do it like i, ii, iii, iv
→ More replies (1)
2
2
2
u/LliLReader Jun 06 '20
Loop variables are generally short lived, so no point in fancy naming unless absolutely necessary.
2
Jun 06 '20
I mean... I wonder if the variables were chosen because i and j are also popular indices for calculus and linear algebra when dealing with sums and matrices. Like.. what were Newton's indices when inventing calc?
2
u/Bastian_5123 Jun 06 '20
It's probably just i = increment, j is the next letter and k is the next letter after that. ijk in maths probably comes from a similar place, except i = imaginary
2
u/takahashi01 Jun 06 '20
I usually use i and k, because my most common and infuriating error was always mixing up i and j
2
2
2
2
2
2
u/Aeium Jun 06 '20
I don't know why everyone does it, but I know exactly why I do it.
i is for iteration.
If you have two nested loops, you can't use i twice (I tried it), so you need to use another letter. j looks like i but it's actually a different letter so it will work.
If you need three nested loops, then k is the next letter in the alphabet, so I guess go with that one.
2
u/klaizon Jun 06 '20
I remember being marked down in college in an introductory programming course for not using c for counter in my loops (as taught but not instructed). I used i, and it was uncomfortable explaining to my professor why i stood for iterator. Some people have hard-coded logic and workflows in their brain that they'll never change.
→ More replies (1)
2
2
u/Nikarus2370 Jun 06 '20
While in college 1 of my professors got quite salty that I was using x and y as variables for a couple for loops that processed over a 2d array that stored map data for a little roguelike i made.
2
2
u/knight_vertrag Jun 06 '20
I think its inspired from the math notation of summation where the terms are subscripted with i,j,k usually.
→ More replies (1)
2
u/jeric_C137 Jun 06 '20
Because counter variables in for loops are temporary so it saves time to not think about it.
2
u/seiga08 Jun 06 '20
No you use n for “number of iterations” and if you need another tone you use m for “mumber of iterations”
2
u/TheGaminja Jun 06 '20
Had a teacher in high school show me resources where they used i, ii, iii, ... That was extreme lawlessness.
2
2
2
2
Jun 06 '20
I always thought it meant 'iterator' and that j was chosen because it looked kinda like 'i'
→ More replies (1)
2
3
Jun 06 '20
Started using 'int index' to impress my crush in office. (You know, the man who cares for his variables will obviously care for his gf/wife)
The nested 'for loop' gave me a headache. In walks 'int jindex'.
2
Jun 06 '20
Pro tips: Use ii instead of a single i. makes finding them so much easier if you e.g. would need to rename your variable.
7
u/wookiee42 Jun 06 '20
No one else on your team is going to stick to that. Plus your code might not throw an error if you accidentally use 'i' or 'iii'. Search for 'for' or 'for (' or ' for ' etc - whatever makes sense in your language. Use the RegEx or boolean logic that your search provides if needed.
→ More replies (2)5
2
u/russellvt Jun 06 '20
Use ii instead of a single i. makes finding them so much easier
Because, those who don't know vim or good regular expressions know nothing about "\s+" and related search patterns.
Notepad++ for Windoze users is also highly recommended, if that's your coding platform of choice... though there are good IDEs out there, too.
2
1
1
1
1
1
1
1
1
1
1
u/rust_neutral Jun 06 '20
What's up with foobar? "Fucked Up Beyond All Recognition" is one hell of a reference to perpetuate all of these years.
1
1
1
1
1
u/Danthekilla Jun 06 '20
i = iterator, index, iteration, incrementor
Pretty much however you look at it i is a great name for a frequently used iteration variable.
And j is just the next in the sequence.
Most people then use k and l. Though it's rare to need code to nest that deeply.
1
1
1
1
u/ForeignerLove Jun 06 '20
i stands for iteration and j..well it just so happens to come after i in the alphabet sequence
→ More replies (1)
1
u/redditk9 Jun 06 '20
I was once told (probably incorrectly) that using ‘i’ as an iteration variable in MATLAB had a performance penalty so now I use ‘k’... you know... for “kount”.
But only in MATLAB. It’s just a habit now for quick MATLAB scripts. Otherwise ‘i’ is where it’s at.
1
1
u/Brothersquid Jun 06 '20
I use i for single for loops but i use r,c for nested for loops. Picked it up in my high school coding class.
1
u/Aldeseus Jun 06 '20
I remember trying to teach my ex python. I had to explain it by showing “for thing in blah”
1
1
1
u/Dummerchen1933 Jun 06 '20
Except for iterating through points in space, then you have to use x,y,z
1
u/thomasdarimont Jun 06 '20
What would you expect from a generation of programmers who got inspired by a guy named D/ijk/stra.
1
1
1.2k
u/holaca9731 Jun 06 '20
Coders that chose the dark side use l