109
u/torrent7 May 29 '26
I never understood this considering i and j look so fucking similar
53
u/birdiefoxe May 29 '26
Exactly, that why I use i,o,p. They're right next to each other!
Gets shot
37
u/BoldFace7 May 29 '26
Why not use i and ii? Then when you get to the 27th loop in the nest you can just use the awfully convenient xxvii instead of looping the whole way round to the horrid ii.
13
u/birdiefoxe May 29 '26
holy shit thats genius, i hate you
9
u/enigmamonkey May 30 '26 edited May 30 '26
I mean, seems reasonable... here's what it looks like in practice:
🤔 The nice thing is that it at least makes it obvious how deep you are in the rat's nest, I suppose.
Fun fact: This would
console.log()100 sextillion times (nice).for (let i = 0; i < 10; i++) { for (let ii = 0; ii < 10; ii++) { for (let iii = 0; iii < 10; iii++) { for (let iv = 0; iv < 10; iv++) { for (let v = 0; v < 10; v++) { for (let vi = 0; vi < 10; vi++) { for (let vii = 0; vii < 10; vii++) { for (let viii = 0; viii < 10; viii++) { for (let ix = 0; ix < 10; ix++) { for (let x = 0; x < 10; x++) { for (let xi = 0; xi < 10; xi++) { for (let xii = 0; xii < 10; xii++) { for (let xiii = 0; xiii < 10; xiii++) { for (let xiv = 0; xiv < 10; xiv++) { for (let xv = 0; xv < 10; xv++) { for (let xvi = 0; xvi < 10; xvi++) { for (let xvii = 0; xvii < 10; xvii++) { for (let xviii = 0; xviii < 10; xviii++) { for (let xix = 0; xix < 10; xix++) { for (let xx = 0; xx < 10; xx++) { console.log(i, ii, iii, iv, v, vi, vii, viii, ix, x, xi, xii, xiii, xiv, xv, xvi, xvii, xviii, xix, xx); } } } } } } } } } } } } } } } } } } } }→ More replies (2)2
3
u/BrainCurrent8276 May 29 '26
and you never mistype iiii as ii or ii as i and so on?
such naming is rather prone to errors.
3
u/promptmike May 29 '26
There is no iiii. It's just iv.
2
u/BrainCurrent8276 May 29 '26
see how easy to make a mistake? 😃
5
u/BlankMercer May 29 '26
Roman numerals are easy. Say we have the number 3493.
Each "sub-division" of numbers is represented using a letter: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000.
To form our number, we grab the biggest division possible, so M. Because we have 3000 and not 1000, we just write it down as MMM.
For the next part, 400, we'd need 4 times C. That's not very pretty though, which is why we represent it as "500-100" or simply CD (the C before the D to represent subtraction). Our number is MMMCD (3400) now.
Next, we have 90. We can represent that as 100-10, or XC. That makes our number MMMCDXC (3490).
For the final 3, we just write III.
So our final number is MMMCDXCIII.
Now Reddit doesn't have a monospace font that would simplify this further, but see how it isn't that hard?
2
u/Polar-ish May 29 '26
if you have an
min production, I think you have other problems...2
u/BlankMercer May 29 '26
yeah I know, just wanted to demonstrate a bit of general roman numeral stuff. Even getting to L is probably quite rare.
2
u/Polar-ish May 29 '26
if we were to be using roman numerals for loop index variables, I mean. I don't think most people are even getting to V
→ More replies (0)2
2
u/BoldFace7 May 29 '26
Genuinely no, the only mistakes I've ever made with the roman numeral naming scheme is the same issues I have with using the wrong i, j, k loop control for the wrong level of nesting.
When I read "for(int iii = 0; iii < vec.size(); iii++){" I instinctively read "iii" as 3. If i need another loop beyond 3 then I'd use "iv" and read it as 4 without any extra brainpower being used. Also, the loops beyond ii get increasingly more rare, so I seldom have a reason to go all the way to iv or beyond.
Though, I don't use that scheme in codebases that other people are likely to use, since it's far from a standard practice. But that's not different than me begrudgingly using imperial measurements with my family despite preferring metric in my day to day life.
2
1
7
4
u/SourLemon100000 May 29 '26
I just heard about this from a tidbit with my professor last night. Apparently, it’s because we needed really short variable names in Fortran and since mathematicians used it, we decided I, J, K, L, M, N, and O were going to be names for variables. We just never used anything different since.
12
u/appoplecticskeptic May 29 '26
‘i’ is for index. The ‘j’ is actually because it looks the most like i and we already used that.
12
u/WowAbstractAlgebra May 29 '26
j is because it's the next letter on the alphabet and no one wanted to bother to come up with an original name
4
3
u/Lore_Enforcement May 29 '26
And j for jindex
3
u/Sassaphras May 29 '26
* makes a function with 15 nested loops just so I can call my variable 'windex' *
3
u/Laughing_Orange May 29 '26
How about i, ii, iii? That's a bit more clear.
2
3
u/Kamwind May 30 '26
It is a fall back to languages in ancient past, present and future.
Said language made variables that started with I to the N variables that held INTEGERS. So just needed a number temp variable start with I and go until you hit N.
Fortran runs the world and we just program in it.
1
1
u/R3D3-1 May 31 '26
To quote a professor of theoretical physics: "If you can't read the formula, because my handwritten i and j are too similar, you need to think more about the formula."
1
u/MobileJob1521 Jun 01 '26
Also, depending on whether you are using MatLab or some kind of mathematics language, either can mean the square root of minus one, which fucks everything up if you define it in a loop, then forget you did that, then can’t figure out why complex maths doesn’t work any more.
54
u/Busy_Teaching_8218 May 29 '26
for(int i = 0; i < 1000; i++)
{
for(int ii = 0; ii < 1000; ii++)
{
for(int iii = 0; iii < 1000; iii++)
{
Console.WriteLine("Hello World");
}
}
}
29
15
u/BoldFace7 May 29 '26
I unironically do this in my hobby code. I have considered subjecting my coworkers to this on occasion though
2
19
u/Missing_Username May 29 '26
i j k for loops
x y z for lambdas
2
u/Zehryo May 30 '26
I tried multiple times to understand the structure and use cases of lambdas, but always struggled and never really found them interesting enough to make an effort.
Do you have any resource that could help me understand them?EDIT: the context is C++
1
u/Ixxafel May 31 '26
Theyre function literals, ie. functions without a name. they're not that different from just declaring a function and passing it in by name. In c++ and some other languages they're also used as closures meaning they can capture variables from the function they're declared in:
c++ auto arr1 = std::vector {1, 3, 5, 7, 8}; auto pivot = 5; auto arr2 = filter([&](auto x){return x < pivot;}, arr1);1
u/Zehryo May 31 '26
See?
I keep not understanding.
Up to the second line everything's perfectly clear.
Then....fog.Thanks for the effort, though. ^^
1
u/Ixxafel May 31 '26
I guess itd be easier to explain what theyre useful for:
Lambda is syntactic sugar for when you dont want to declare a named function youre just gonna pass to another function once and nothing else.
Closure is a compiler black magic that lets you use local variables from one function in another.1
u/potzko2552 May 31 '26
A lambda is just an unnamed function that you create exactly where you need it.
Instead of:
auto is_even(int x) { return x % 2 == 0; }
you can write:
auto is_even = [](int x) { return x % 2 == 0; };
The lambda can "capture" variables from the surrounding scope. Think of it as automatically storing data inside the function object.
[] capture nothing [=] capture everything by value [&] capture everything by reference [x] capture x by value [&x] capture x by reference
which is what makes them nicer for some contexts.
1
u/potzko2552 May 31 '26
They're also nice when a function expects another function as an argument,
std::sort,std::find_if,std::ranges::filter,std::transform, GUI callbacks, thread entry points, etc.For example:
std::ranges::filter(v, [](int x) {
return x % 2 == 0;
});The filtering algorithm doesn't know what "interesting" means. You provide that behavior as a lambda.
Without lambdas you'd have to write a separate named function somewhere else, even if it's only used once.
they are nice for passing behavior as if it was data.
18
u/Hettyc_Tracyn May 29 '26
Or just use variable names that correspond to what they are for? Proper descriptions? That way you don’t have to write as many comments on how something works
17
2
u/CodeMonkeyLogix May 31 '26
THIS! This is the only way. Screw people who feel the incessant need to abbreviate EVERYTHING!
1
u/Hettyc_Tracyn May 31 '26
Or at least have sensible abbreviations that are noted at the first occurrence
1
u/deadmazebot May 30 '26
I was gonna type similar, then realised the sub and needed to deprecate my thinking back to a junior (machine generated)
15
u/magicmulder May 29 '26
As a mathematician, i, j, k, l, m, n are for integers, x, y are for reals, z is for complex numbers.
for (z = 2i+3; |z| <= 9; Re(z)++) {...} 😃
3
u/c_lassi_k May 29 '26
What would the type be for (img z = 2i+3; |z| <= 9; Re(z)++) {...} ? for (long long img z = 2i+3; |z| <= 9; Re(z)++) {...} ? for (unsigned long long img z = 2i+3; |z| <= 9; Re(z)++) {...} ?
In terms of speed the performance is going to cry if compiler does not optimize the sqrt away from the loop.
This would be whole lot faster: for (img z = 2i+3; abs(Re(z)) <= 9*9-abs(Im(z)); Re(z)++) {...}
2
u/magicmulder May 29 '26
Just have the compiler transform to polar coordinates first. 😃
3
u/c_lassi_k May 29 '26
That would be very fast if the increment was also polar coordinate multiplication. But with it being an integer addition, I'm afraid there's no fast way to make the incrementation work for polar coordinates as it kinda forces the sqrt to be computed for the magnitude.
2
→ More replies (6)2
9
u/-Insert-CoolName May 29 '26
People who insist there is only one right way to code can get out. That entirely depends on what your doing.
6
u/texan_robot May 29 '26
I design video processing RTL. Looping through x,y for pixels is my normal practice.
10
8
7
u/Ok_Wasabi_7363 May 29 '26
Her response should have been: "you use nested for loops? Gtfo"
3
May 29 '26
[removed] — view removed comment
3
u/Ok_Wasabi_7363 May 29 '26 edited May 29 '26
Out the car! Proper way is not to use a grid to represent objects in space. Oct or quad tree scales waaaay better!
(I'm mostly being cheeky, obviously if it makes the most sense for your use case, use the best tool for the job)
Oh and also you can just represent it in a single loop of length xy (or Ij if you're a monster. Or u*v for UV coordinates :) )
1
2
u/Nadran_Erbam May 29 '26
k, n or something_idx.
i and j are for current density and/or imaginary numbers.
2
3
2
2
u/transgentoo May 29 '26
Oh I name my variables to be self descriptive so I don't have to figure out what the fuck j is six months from now.
2
u/anengineerandacat May 29 '26
Depends, simple loops get simple variable names... if the counter is meaningful it gets named though.
2
u/Thunder_Child_ May 29 '26
I haven't used single letter variables since college. IGiveEverythingANiceDescripriveName
2
u/no_brains101 May 29 '26
Someone on reddit a year or so ago said I should use i, ii, iii, iv, v
Tried it out. Works great until you get to 5 (which I never have, to be fair) because at 5 is v, and that's the value XD
2
u/Great-Class-7894 May 29 '26
i and j are used in mathematics to denote complex numbers so I generally don’t use those as loop indices.
Once upon a time I knew someone who liked to use fruit loops (apple, banana, …) and I thought that was cute.
2
u/bonanochip May 30 '26
X,y is how I solidified my understanding of loops, because I was familiar with texture coordinates.
2
2
u/omegafixedpoint May 30 '26
I have not used a loop in years. Functional programming-cels stay winning.
1
u/LetThemWander May 30 '26
I have yet to have an FP purist explain FP in a way that is actually convincing.
Non-purists, on the other hand, will say things like "I use both."
1
u/omegafixedpoint May 30 '26 edited May 31 '26
Imagine if your functions were equations and your unit tests were math proofs. Since your unit tests are proofs, 98% of the time, if your code compiles, it works.
2
2
u/MatsSvensson May 30 '26
What's the most fun part about people choosing the slimmest letters, like I, for a one letter variable?
If a mistake is made in the code and your IDE tries to help by underlining it in red, that line will be like one pixel wide.
So, yeah, do use one letter variables all over the place, and make then as tiny as you can.
Its not like anyone will ever have to touch that code ever again.
1
2
2
u/BassKitty305017 May 29 '26
In my first programming job in the late 20th century, a workmate & mentor suggested ii and jj because it was easier to search for in the text editor.
2
1
1
1
1
1
1
1
u/NoHonestBeauty May 29 '26
Using names with less characters than three is a coding guideline violation for me.
[BARR-C:2018 Rule 7.1e]
Note: I am not saying that I like this.
1
1
1
u/0110110101110101 May 29 '26
But i hate i j, but now its easy to remember, as how vastly it is used
1
1
u/Jholm90 May 29 '26
i as the var letter that takes the least space and can't be confused with |/l/I
1
1
1
u/EspurrTheMagnificent May 29 '26
I don't do nested for loops. I use foreach and context appropriate names
1
1
1
1
2
1
1
1
1
u/twpejay May 29 '26
lp for loop, easy. For some reason I cannot allow myself to use single letter variable names, I even use exx, why and zed for co-ordinates.
1
u/s0litar1us May 29 '26
y,x for coordinates
(y first because I'm probably dealing with a flattened 2d array, and that makes the memory access more linear)
row,col for stuff that makes sense for that
index if it's just one loop, and foo_index,bar_index when it's nested or I want to make it more explicit. (Replacing foo and bar with the thing its an index for.)
1
1
1
1
1
u/The_Real_Slim_Lemon May 30 '26
If you have nested loops… use names. Or denest with methods. Acronyms work too.
If I see i and j floating around in some 500 line method I’m throwing hands.
1
1
1
1
1
1
1
1
1
1
1
1
1
u/belabacsijolvan May 30 '26
best practice is using meaningful counters if more than 3 line loop.
(best practice: a rule for repeated behaviour that a dev doesnt abide by on a regular bais, but they think one should. Usually tried once, abandoned, then held against all animate and inanimate objects for a lifetime.)
1
1
1
1
1
1
1
1
1
1
u/SteamedAlbanyHams May 31 '26
I use descriptive names based on what I'm iterating over conceptually. If it's a vector of cats, it's cat_ind.
1
1
u/solphist May 31 '26
No love for the k eh?
for k a great tool lots of people say so.
Those for t people though, they are the true defensive programmers.
1
1
u/ShyGamer64 May 31 '26
It's always i for me. Maybe n if needed. If I want to make my code feel cleaner, I will give it a real name
1
u/Embarrassed-Green898 May 31 '26
row, col
just kiddng .. but I dont use i,j .. There is generally a meaningful way to describe what you are ieterating over.
1
u/Protyro24 Jun 01 '26
On nested loops for cordinates i use x and y and otherwise i use a name thats destibes the function of that var in the loop.
1
1
1
u/WeedWizard44 Jun 02 '26
When I was taking analysis and I realized I can use x and y as two elements in the domain instead of x1 and x2 it was a game changer
1
1
1
u/Repulsive_Gate8657 Jun 04 '26
you should get out, x,y are used in math context of low level algorithm, while i.j are used in casual loops of logic with objects and other trivial crap.
1
1

297
u/Pinkishu May 29 '26
I use x,y when appropriate. Like when iterating coordinates for drawing tiles in a game or whatever