I always skip j and l to avoid possible cock ups with that, or with misreading. I always go i, k, m, n and while it does not really make a difference I honestly think it makes it a tad easier to read.
It's not really 4+ loops deep, but if I call a function to work the deeper loops, I like to keep the index names constant through the calls. This leads to the point where a certain function may only have one loop but is already the 6th loop so I loop through p isntead of i (which is also no issue with pointers or anything since it's .NET code and doesn't have points anyway).
Is it necessary? Nah. Is it always doable? Nah, obviously some programs run way too deep to keep it up. But when I cycle through part(1), program version(2), batch(3), and testrun(4) to throw a day worth of QA results into an Excel table it works.
This sub jokes a lot about "self explainnatory" and "self documenting" code and while I think we all agree you still need comments, I always felt that making variable and index names unique within reasonable scope massively reduces the possibility for mistakes / eases understanding the code for everyone.
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.
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.
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.
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.
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.
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.
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.)
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
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.
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.
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.
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.
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.
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)
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.
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.
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
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.
I use it for 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.
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.
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.
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.
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.
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?
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
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.
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.
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.
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.
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.2k
u/holaca9731 Jun 06 '20
Coders that chose the dark side use l
1.5k
u/Kooneybert Jun 06 '20
The iteration variable makes sense to be called i. j is just the next number in alphabet.
130
u/[deleted] Jun 06 '20
You know the rules and so do I
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.
190
u/[deleted] Jun 06 '20
I always used "i" as "index", "j" as "jndex", "k" as "kndex"...
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.
356
u/[deleted] Jun 06 '20
Programmers: name of variable should be self explaining what variable is for
Also programmers: use i,j,x,y,z 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.
45
u/qwasd0r Jun 06 '20
row, col, idx... I'm lawless.
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.
17
u/bonbonlable Jun 06 '20
I sometimes use n and k but mostly in Matlab only š
17
u/[deleted] Jun 06 '20
*laughs in functionals*13
u/dependency_injector Jun 06 '20
I use x and y if the array represents something surface-like
25
u/li-si Jun 06 '20
Homage to Dijkstra.
11
u/Noch_ein_Kamel Jun 06 '20
iterator
jitterator
kidzbopirator
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
u/[deleted] Jun 06 '20
Because i is the index and j is the next letter in the alphabet.
5
u/DownshiftedRare Jun 06 '20
"i" is for incrementor.
"j" is for Joe, who really likes incrementors.
11
u/Michel3951 Jun 06 '20 edited Jun 06 '20
x, y anyone? No?
edit: lowercase x,y
4
u/magoomba92 Jun 06 '20
What the foo()
4
u/Ta1sty Jun 06 '20
We got it from the mathematicians
4
u/almarcTheSun Jun 06 '20
What's the show? This looks so familiar.
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.
3
u/[deleted] Jun 06 '20
Iterator jiterator baby
3
u/[deleted] 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
u/Ivesy3000 Jun 06 '20
This is the way.
4
u/yharnamite_blood Jun 06 '20
Once I tried to use h,k as variables, The FBI was at my doorstep, TRUE STORY.š§
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
u/[deleted] Jun 06 '20
I'm more of a k and c type.
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
u/CageAnElephantPLZ Jun 06 '20
ppl who use x and y, YIKES
2
u/MrJod2002 Jun 06 '20
Am I the only one who uses iās and xās?
2
u/LliLReader Jun 06 '20
Loop variables are generally short lived, so no point in fancy naming unless absolutely necessary.
2
u/[deleted] 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/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
u/589ca35e1590b Jun 06 '20
I use x and y come at me
2
u/kazneus Jun 06 '20
summations over i,j,k for 3d matricies. This comes from math.
2
u/llagerlof Jun 06 '20
Spotted the java programmer.
2
u/ahughezz Jun 06 '20
I need my iterators and jiterators
2
u/Mc_UsernameTaken Jun 06 '20
I use C, just so I can write C++
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
cforcounterin my loops (as taught but not instructed). I usedi, and it was uncomfortable explaining to my professor whyistood foriterator. Some people have hard-coded logic and workflows in their brain that they'll never change.2
u/StereotypicalCat Jun 06 '20
i = index/indicies?
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
u/rjsh927 Jun 06 '20
I also use k sometimes. I am daring like that.
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.
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
u/SherlockSilicon Jun 06 '20
Index and jindex of course
2
u/_demoncleaner Jun 06 '20
I personally prefer variables over variabes.
2
u/[deleted] Jun 06 '20
I always thought it meant 'iterator' and that j was chosen because it looked kinda like 'i'
2
u/[deleted] Jun 06 '20
iterator jiterator kiterator, duh.
3
u/[deleted] 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
u/[deleted] 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.
1
u/TreadheadS Jun 06 '20
i is "index" or "iteration", either makes perfect sense. j is just i++
1
u/cdreid Jun 06 '20
Also a and n . If you do otherwise youre an anarchist and We Are Watching You!
1
u/[deleted] Jun 06 '20
Professional have standards
1
u/AvenDonn Jun 06 '20
If you use anything other than x in your lambda we can't be friends
1
u/LepruconX Jun 06 '20
Yeah I donāt know why I do this too maybe I saw my teacher do it?
1
u/TheMogician Jun 06 '20
It's funny because I always used m and n.
1
u/james_harushi Jun 06 '20
i = integer
j = just another integer
1
u/Speedster4206 Jun 06 '20
Whatās the default IDE? Notepad??
1
u/-Listening Jun 06 '20
I also want to know the law?
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
u/[deleted] Jun 06 '20
Index, jndex
1
u/Stalematebread Jun 06 '20
I for either iteration or index, j as the next letter
1
u/[deleted] Jun 06 '20
Increment, index, iteration..
1
u/skhiari1994 Jun 06 '20
i and j is like pissing and shitting on human being
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
u/Bollenisback Jun 06 '20
i = increment, iteration, index j = neighbor
1
u/thataccountforporn Jun 06 '20
I mean Index and Jndex, you have to use them!
1
u/scifiburrito Jun 06 '20
me, getting blasted for using n
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
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
u/wowy-lied Jun 06 '20
I use "iter" before "I".
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
u/Sbvv Jun 06 '20
Please, stop doing that, I want refactor easily your s***
1
u/Speedster4206 Jun 06 '20
Itās going up in the ole cubicle
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
u/letss_Do_thiss Jun 06 '20
What about x,y and k
1
u/Assasin2gamer Jun 06 '20
"It's my opinion, just like a holocaust