Sure just make sure you use x for column and y for row, which means using y in the outer for loop because if you do it the other way you will confuse yourself and anyone else that ever reads it.
I kinda disagree? Row/column doesn't even mean anything for arrays, only inner and outer. I say do what makes sense for your context and is cache performant.
If we’re talking about coordinates for drawing game tiles (the above comment) then it matters. Sure you could name the axes whatever you want, but in math, drawing, and engineering, x is left to right and y is up and down (if you’re looking down at a page, z would be lifting off the page or height for engineering)
I think the point was that the memory layout of rows and columns is arbitrary, so whether you iterate over rows and then columns or columns and then rows (assuming you care about memory locality) depends on your use case.
e.g. in Minecraft, block data is stored in vertical columns, with X and Z being the horizontal coordinates, so you may well iterate over the Y coordinate last.
True but the standard for screens is to go through an entire row then go to the next column when rendering or scanning on a monitor so it would probably look strange if you’re iterating over columns then rows
Doom sprites are also stored as columns of pixels, as I recall, just to give another counter-example.
For your garden variety 2D blitter, sure, rows then columns is usually what you want. But that is only one small corner of the graphics programming space.
In higher level mathematics i, j, k (typically denoted with hat on top but keyboard limitations) are sometimes used as a more generalized set of perpendicular directions instead of the x, y, z we all know and love
It really only matters if you need to iterate over the space in a particular order. Otherwise you can have either in the inner/outer loops since your index is always gonna be [x][y] or x + y * xMax for a 1D array
x for column and y for row, which means using y in the outer for loop
first statement is true, but second statement doesn't follow from first statement.
Whether you best loop row-by-row or column-by-column, depends on the situation.
Personally I would use x and y only for pixels on the screen, and when it's cells in a grid for example, I'd use i and j, so that there will be no confusion if I have to transform these coordinates to pixels.
If you think of i and j as unit vectors on a plane (which is why we call them i and j i believe) and their values their magnitudes respectively, they make more sense
<i, j> notation is standard notation when reasoning about discrete, indexed contexts on an x, y plane (which for loops are) in mathematics and, obviously, computer science. it has the added, inextricably related benefit of adhering to the matrix notation which is standard for linear algebra. this can open problems to geometrical problem solving which would otherwise require more difficult analytical reasoning
Definitely agree. The notation lines up. Look at summation notation and the variable it uses (i) and the variable used when another summation is added (j). I think the schism in opinions in this post is greatest between the data/mathematical folks and the more general developers/engineers. I started on the latter side but I'm firmly in the former now, so I tend to keep it direct with the mathematical formulation.
I don't write graphics software, so I am almost never representing coordinates in any loop I write.
But say I were writing a game or some geometry-based thing, then yes, I think x and y are appropriate.
I'm also being a little facetious: I usually use i like every body else. However, I also avoid nesting code blocks as much as possible. So if there's an inner loop, I'm usually offloading that inner loop to its own function/method with a descriptive name so that it's easier to read when I'm drunk.
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.
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);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
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.
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?
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.
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.
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."
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.
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?
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:
auto arr1 = std::vector {1,3,5,7,8};auto pivot =5;auto arr2 =filter([&](auto x){return x < pivot;}, arr1);
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.
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.
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
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)++) {...}
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.
☝️erm actually integers are real numbers and real numbers and complex numbers. so according to your definition x, y, z would also be used for integers and z would also be used for real numbers.
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.
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.
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.)
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.)
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.
Why is i capitalized Scared. I,l,1 and O,0 are too similar.... also yeah I use i,j but if I have complex literals about I might go straight to i,k for clarity
298
u/Pinkishu May 29 '26 •
I use x,y when appropriate. Like when iterating coordinates for drawing tiles in a game or whatever
110
u/torrent7 May 29 '26
I never understood this considering i and j look so fucking similar
54
u/Busy_Teaching_8218 May 29 '26
21
u/Missing_Username May 29 '26
i j k for loops
x y z for lambdas
19
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
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.
16
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)++) {...} 😃
6
u/texan_robot May 29 '26
I design video processing RTL. Looping through x,y for pixels is my normal practice.
10
u/Far_Understanding883 May 29 '26
You guys write your own loops?
1
u/Klaus1164 May 30 '26
Oh, Jesus, it was only a meme, but I started a fire.
9
u/MonkeyManW May 29 '26
People who use a, b, c, d are war criminals
7
u/Ok_Wasabi_7363 May 29 '26
Her response should have been: "you use nested for loops? Gtfo"
3
u/Dillenger69 May 29 '26
i = iterator
ii = inner iterator
2
u/Nadran_Erbam May 29 '26
k, n or something_idx.
i and j are for current density and/or imaginary numbers.
2
u/WorthBite5254 May 29 '26
I either use actual names or roman numbers
2
u/Azurelion7a May 29 '26
I use whatever I want.
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
u/AlexisExploring May 30 '26
If im using a linear equation or graphing x, y. But loops i, j is law
2
u/omegafixedpoint May 30 '26
I have not used a loop in years. Functional programming-cels stay winning.
2
u/expo78 May 30 '26
i dont know why i always use p and q, but i got used to using them
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.
2
u/JesusTheSecond_ May 30 '26
are we really discuting variable names ?
2
u/blizzardo1 May 29 '26
X and Y for coordinates only, I and J for indexing.
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
u/skildert May 29 '26
l?
1
u/floriandotorg May 29 '26
n,m is obviously superior.
1
u/jonfe_darontos May 29 '26
foo, bar, biz, baz, fred
1
u/Upbeat-Education2117 May 29 '26
Good 'ol jndex, nothing beats that
1
u/StabbyBlowfish May 29 '26
I use n,m
1
u/Lost_Possibility_647 May 29 '26
I use index and then index2
1
u/hehesf17969 May 29 '26
ii jj kk because Matlab
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
u/flipswab May 29 '26
Just use j
1
u/JTJdude May 29 '26
Letters and numbers in math is always gonna confuse some people.
1
u/0110110101110101 May 29 '26
But i hate i j, but now its easy to remember, as how vastly it is used
1
u/SysGh_st May 29 '26
a, b
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
u/LJBear99 May 29 '26
Thats me! -lj
1
u/goos_ May 29 '26
the I,j is irrationally angering me
1
u/mplaczek99 May 29 '26
tf? It was always i and j for nested loops
1
u/MonkeyDDataHQ May 29 '26
If it's not a descriptive name, I already hate you.
1
u/Goofy_Gunton May 29 '26
What do you use for tables?
1
u/Due_Incident_2356 May 29 '26
i and n
1
u/Ebula-The-Duck May 29 '26
iterator and jterator
1
u/rix0r May 29 '26
I like to use a, i, z
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
u/elreduro May 30 '26
I thought that x is standard in python
1
u/LiteratureLow4159 May 30 '26
I use i and j :)
1
u/Ursomrano May 30 '26
Is it the first for loop of the file? I use a. 2nd? b. And so on. /hj
1
u/the-judeo-bolshevik May 30 '26
Itr_Descriptive_Name
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
u/DestinationVoid May 30 '26
index, jdex, kdex...
1
u/Vamosity-Cosmic May 30 '26
1
u/CGI42 May 30 '26
Who the FOOK uses capital i????
1
u/Karoolus May 30 '26
i for integer loops
s for string loops
Actual names when needed
1
u/babalaban May 30 '26
who tf uses
L?1
u/No_Cartographer_6577 May 30 '26
I use i for index now so anything but I works
1
u/Ok-Hotel-8551 May 30 '26
I,j are wrong as well
1
u/WhyShouldIStudio May 30 '26
i use a,b
1
u/VisualAlive1297 May 30 '26
i ii iii iv v…
1
u/horenso05 May 30 '26
row, col i, j outer, inner x, y
just whatever is the clearest
1
u/Significant-Cause919 May 30 '26
I, ii, iii, iv
1
u/romhacks May 30 '26
baby's first programming meme
1
u/FuckedUpYearsAgo May 30 '26
You guys still write code by hand? Smirk.
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
u/Straight-Ad5775 May 30 '26
Well well
1
u/1337_w0n May 30 '26
for (n<N){for (m<M)} ☺️
1
u/xThomas May 30 '26
We really arguing about single letter variables here :(
1
u/Outside_Volume_1370 May 31 '26
L and J? He should get out of the car too
1
u/justinknowswhat May 31 '26
index and jindex
1
u/Linedriver May 31 '26
Index, subindex
1
u/CytroxGames May 31 '26
I use I,J,K depending on how many loops are needed.
1
u/ByteBandit007 May 31 '26
ss,ssdd
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
u/HaroerHaktak May 31 '26
Sir. It's R and L
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
u/Mockingbird_ProXII May 31 '26
for i, xi in enumerate(x):
for j, yj in enumerate(y):
😛
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/Nargarinlok Jun 02 '26
n & m of course...
1
u/lopar4ever Jun 02 '26
c, because I could use c++ :D
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
u/LegisLab Jun 03 '26
I use i,n
1
u/Pasha82831 Jun 03 '26
I think I am alone who using i, i2, i3, ...
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
u/New_Tomorrow_4847 Jun 04 '26
sus, sus2, sus3
1
u/BladeMaster7461 Jun 08 '26
i use n, x
1
u/Dependent-Feature-68 May 29 '26
"I use x, y for arrays and i,j for matrices"
"But they're practically the same"
"Now your turn to get out"
1
u/Consistent_Payment70 May 29 '26
It feels wrong to use i and j on Computer Vision fields. I used (x, y) or (w, h) sometimes.
0
u/oxwilder May 29 '26
I use o, t for one and two. I skip three, five, and seven.
0
u/RedAndBlack1832 May 29 '26
Why is i capitalized Scared. I,l,1 and O,0 are too similar.... also yeah I use i,j but if I have complex literals about I might go straight to i,k for clarity
0
u/Syseks May 29 '26
й
-1
u/dtb1987 May 29 '26
I use p,q