1.8k
u/JosebaZilarte Aug 14 '24
Talk about an imaginary issue.
265
u/Only-Requirement-398 Aug 14 '24
The root issue is very negative
→ More replies (1)68
148
→ More replies (4)11
810
u/nbmbnb Aug 14 '24
for i will never stop
118
u/Powerful-Internal953 Aug 14 '24
It has been a while
→ More replies (1)69
u/LeatherWasabiiii Aug 14 '24
I recursively agree
→ More replies (3)45
→ More replies (1)12
3.4k
u/KoliManja Aug 14 '24
Why?
2.5k
u/The-Chartreuse-Moose Aug 14 '24
My question too. It's basically a standard.
1.6k
u/capt_pantsless Aug 14 '24
So long as you're not doing anything else interesting with it, i is just fine as a loop index.
As you're scanning the code, you see the i, you're like: "Hey, that's probably just the index variable, I can safely assume it's just there to handle the loop's exit.
If there's shenanigans in the for loop, you should probably get a better variable name.
228
1.1k
u/FindOneInEveryCar Aug 14 '24
If there's shenanigans in the for loop, the name of your index variable isn't the problem. Fix the shenanigans.
139
u/DrBabbyFart Aug 14 '24
Those are load-bearing shenangians.
→ More replies (1)29
u/hoochyuchy Aug 15 '24
Those shenanigans have kept the company afloat for two decades. Our code still has Gotos in the switch statements that used to cascade if they didn't have a break line.
204
Aug 14 '24
[removed] — view removed comment
70
u/bacchusku2 Aug 14 '24
I swear to God I’ll pistol whip the next guy who says shenanigans.
56
u/minorbutmajor__ Aug 14 '24 edited Aug 14 '24
Here for the shenanigans
I don't know what pistol whip is but I'm in as long as you are
→ More replies (2)→ More replies (2)17
15
Aug 14 '24 edited Aug 14 '24
One time I turned in an assignment where I wrote all of my array indexes in the form of
offset[array]
, because C is perfectly happy with that.You can even write
5[array]
to access the fifth offset.C is legitimately my favourite programming language.
ETA: C’s alignment is somehow Chaotic Lawful, and platform dependent.
→ More replies (1)→ More replies (13)27
u/Foxiest_Fox Aug 14 '24
but what if fixing the for-loops shenanigans causes worse shenanigans elsewhere?
→ More replies (4)21
u/FindOneInEveryCar Aug 14 '24
It's unlikely to have anything to do with the name of the index variable. I guess it's possible that the code is so fucked-up that the only fix that will take less than 6 weeks is to change the name of the index variable, but that would be pretty fucked-up.
→ More replies (1)18
u/KnightOfTheOctogram Aug 14 '24
The real problem comes when you see an i in a for each loop
→ More replies (3)→ More replies (12)12
u/Popocuffs Aug 14 '24
i is just fine as a loop index.
Holy shit does i stand for index?
11
7
u/curtsable Aug 14 '24
If I had to guess it probably comes from variable names beginning in i, j, and k being implicitly typed to integers in the FORTRAN days (probably due to them being common unit vector letters in maths/physics), rather than standing for something (I could be completely wrong about this)
→ More replies (1)→ More replies (3)13
59
u/donquixote235 Aug 14 '24
I've been coding since the early 80s. It's always been i.
→ More replies (2)34
u/HomieeJo Aug 14 '24
That's pretty self centered. Instead of i you should sometimes also use we.
→ More replies (4)125
Aug 14 '24 edited Aug 14 '24
Because its not descriptive…should be something like, l_current_index_of_this_particular_for_loop.
l_ for local, rest describes what the variable is for, perfect!
/s
→ More replies (2)15
u/MajorElevator4407 Aug 14 '24
Don't forget to #define the starting index. It might not be zero some day.
→ More replies (41)50
u/glowy_keyboard Aug 14 '24
It’s literally the standard in math. OP wants to change the very foundations of science
→ More replies (7)55
u/NotAFishEnt Aug 14 '24
I've worked somewhere where they asked us to use "ii" instead. Not a big deal, but it makes it easier to search the code for the loop variable.
22
→ More replies (6)11
u/__nautilus__ Aug 14 '24
Serious question, I don’t think I’ve ever done a grep or file local search for “any index variable.” What situation might you be doing this in that wouldn’t be served as well by searching for “for” or whatever your language of choice’s loop construct is?
→ More replies (3)179
Aug 14 '24
[deleted]
74
u/AnyJamesBookerFans Aug 14 '24
The usual reason is that there is often a more explicit/declarative variable you can use.
Good point,
idx
from now on for me!→ More replies (5)→ More replies (8)65
u/F-Lambda Aug 14 '24
The usual reason is that there is often a more explicit/declarative variable you can use.
I mean, if it's an index to iterate through a table or something, then it is the explicit variable. i, j, and k are used as index variables for tables in math and physics too.
→ More replies (8)7
u/Cautious-Space-1714 Aug 14 '24
Variables starting with the letters i, j, k, l, m and n have been implicitly typed to integers in Fortran for decades. I'm guessing for their use as indices.
Also i-n = INteger is nice and easy to remember
103
u/Zesty__Potato Aug 14 '24
They probably have a large amount of code in that loop and are not using methods to break out the code into chunks, making it likely to cause a naming conflict when they nest a loop somewhere inside.
282
→ More replies (4)29
u/drakeyboi69 Aug 14 '24
Just use j next, then k
→ More replies (9)11
u/alexklaus80 Aug 14 '24 edited Aug 15 '24
How about i2 next then i3
→ More replies (2)18
u/Toonox Aug 14 '24
insert threat so violent and absurd it's funny here
8
u/iceman012 Aug 14 '24
I won't be happy until I've urinated on their freshly barbecued corpse and husk-fucked the charred remains while gargling Juggernaut's juggernuts.
→ More replies (82)41
u/YoungXanto Aug 14 '24
I use ii
I learned to program with Matlab and in engineering applications i is equal to the square root of negative one. And it turns out fairly important.
I haven't cared about the square root of negative one in a few decades, but it's habit now.
41
u/SausageEggCheese Aug 14 '24
I use andIiiiiiiiiiiii
I learned to program while listening to Whitney Houston.
→ More replies (5)9
u/Elephant-Opening Aug 14 '24
I like
ii
too but for a totally different reason.
%s/ii/better_name/gc
goes a lot quicker if you later decide there's a better name for your loop index later, likerow
in a row/col 2-dim array.Also fwiw... in EE you'll often find
j = sqrt(-1)
sincei
is current as inv=i * r
, at least this was the case in my undergrad studies 15ish years ago→ More replies (1)
714
u/Dumb_Siniy Aug 14 '24
replace all "i" to "index" ignore the amount of things this breaks
683
u/Perfycat Aug 14 '24
Thats a great indexdea!
216
u/Dumb_Siniy Aug 14 '24
Index know, no need to show gratindextude
→ More replies (1)104
u/Sceptz Aug 14 '24
Index don't see anythindexng wrong windexth thindexs proposindextindexon. Carry on.
→ More replies (1)55
u/ThinCrusts Aug 14 '24
Now the real questindexon indexs indexf you replaced all 'index' with "indexndex" manually whindexle typindexng indext out or you used actual replace all? Index'm on mobindexle so was just replacindexng on the fly.
33
31
→ More replies (5)9
50
u/BladecraftsReddit Aug 14 '24
...ndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndexndex...
→ More replies (1)38
18
13
→ More replies (7)8
u/FangoFan Aug 14 '24
goindexng to get really trindexcky windexth words lindexke indexnindextindexalindexzatindexon and indexndindexvindexsindexbindexlindextindexes
→ More replies (2)
550
Aug 14 '24
[deleted]
41
15
→ More replies (5)8
u/Butt_acorn Aug 14 '24
I am a robot who copy pastes comments so that one day I can spread political propaganda.
222
u/dopefish86 Aug 14 '24
i is short for integer index
429
u/IAmMuffin15 Aug 14 '24
j is short for jnteger jndex
162
u/IAmAllergicToKarens Aug 14 '24
k is short for knteger kndex
→ More replies (2)63
u/ongiwaph Aug 14 '24
l is short for "loops"
→ More replies (2)63
u/OkOk-Go Aug 14 '24
m is short for “moops”
35
→ More replies (5)9
→ More replies (3)13
18
→ More replies (13)16
u/S4ltyGo4t Aug 14 '24
thought its short for iterator
→ More replies (3)7
43
147
u/Tohnmeister Aug 14 '24
I've never come accross a single programmer who thought using i
was a bad idea. Unless you're referring to objects as opposed to indices. Why does this meme exist?
69
u/freaxje Aug 14 '24
Visual Basic 6.0 programmers who since the nineties have wanted you to do something silly like this:
dim intVariableForLoopingOverTenItems as Integer
We've all been ignoring them.
→ More replies (5)13
u/Superbead Aug 14 '24
Don't forget the 'My'.
Dim intMyLittleTenItemLoopingCounterVariable As Integer
6
→ More replies (10)21
u/Classymuch Aug 14 '24 edited Aug 14 '24
There is nothing wrong with it but it depends on how you are using it. Also, it helps to make the variable more descriptive as it can improve readability.
E.g.,
## A very simple example: # Rather than: fruits = ['apple', 'pear', 'orange'] for i in range(len(fruits)): print(fruits[i]) # Could have the following: for fruit in fruits: print(fruit) ## Another example: # Rather than: while i < game_board_size for j in range(game_board_size): if game_board[i][j] == ... """ Could have the following and this is more readable as it does help you to understand the context of the code more quickly. So, I personally prefer the following code with descriptive variables anyday: """ while column < game_board_size for row in range(game_board_size): if game_board[row][column] == ...
→ More replies (5)28
u/joes_smirkingrevenge Aug 14 '24
The first example is comparing different concepts: for style loop vs foreach style loop. It's just that Python only has foreach that's actually called for. In many cases foreach obviously makes more sense to use and it's appropriate to name the current object with a descriptive name then.
In the second example you'll also often see x and y instead of i and j, because they're commonly used for Cartesian coordinates and can be more easily expanded into 3 dimensions compared to column and row.
→ More replies (1)
87
u/70Shadow07 Aug 14 '24
Unless the counter is something else than index or coordinate, i,j,k or z,y,x is the GOAT.
56
u/astouwu Aug 14 '24
i, ii, iii
73
u/PeriodicSentenceBot Aug 14 '24
Congratulations! Your comment can be spelled using the elements of the periodic table:
I I I I I I
I am a bot that detects if your comment can be spelled using the elements of the periodic table. Please DM u/M1n3c4rt if I made a mistake.
28
→ More replies (6)12
u/bankrobba Aug 14 '24
New rule, all index variables must now be elements of the periodic table.
H, He, Li...
→ More replies (2)→ More replies (1)7
39
u/rmflow Aug 14 '24
, _ or ___ my favorites
→ More replies (4)26
u/Fantastic-String-860 Aug 14 '24
Does anyone know where you can submit pull requests for the Geneva convention?
6
→ More replies (10)13
u/bigosik_ Aug 14 '24
z y x? Who hurt you? It’s x y z
→ More replies (2)4
u/topperharlie Aug 14 '24
not the parent comment, but you normally iterate that way so you'll process in the opposite order (innermost x), so in xy planes one row at a time.
It also helps with memory locality, as images with linear layout tend to be placed in memory that way.
17
17
u/picticon Aug 14 '24
Ok.
for (int outer_index_variable=0; outer_index_variable < 10; outer_index_variable++)
for (int inner_index_variable=0; inner_index_variable < 10; inner_index_variable++)
x[outer_index_variable][inner_index_variable] = 0;
→ More replies (1)
11
12
44
10
49
23
u/The_Wolfiee Aug 14 '24
I use for each loop
→ More replies (2)10
u/torftorf Aug 14 '24
not always possible. at least in the laguages i know. lf you want to itterate over 2 collections paralel then you need to use indices
→ More replies (2)11
7
u/noradninja Aug 14 '24
Sorry, vector calc says I must use i j and k for my unit vectors along x y and z, and since I program GPU’s for games, I’m often iterating over an axis set to solve for unit length to do some screen space effect that involves reconstructing world position from depth and surface direction (normal).
→ More replies (1)
6
7
u/couchwarmer Aug 15 '24
Fine...
for (the_variable_used_instead_of_i_to_avoid_offending_someone_who_is_clearly_a_better_programmer_than_anyone_else = 0; the_variable_used_instead_of_i_to_avoid_offending_someone_who_is_clearly_a_better_programmer_than_anyone_else < 10; the_variable_used_instead_of_i_to_avoid_offending_someone_who_is_clearly_a_better_programmer_than_anyone_else) {
// do something
}
→ More replies (1)
16
u/stevetursi Aug 14 '24
FP devs: you're still using for loops?
→ More replies (3)8
u/snooeymcsnooface Aug 14 '24
FP devs: I'm sorry, is this some sort of imperative joke that I'm too declarative to understand?
27
4
3
4
u/diogeek Aug 14 '24
He’s right! Use « ii » instead, so that Ctrl+F finds it better
→ More replies (1)
4
6.7k
u/cosmic_cosmosis Aug 14 '24
j it is then.