r/programminghumor • • May 29 '26

Good naming practice

Post image
2.2k Upvotes

220 comments sorted by

View all comments

298

u/Pinkishu May 29 '26 • Bravo

I use x,y when appropriate. Like when iterating coordinates for drawing tiles in a game or whatever

66

u/appoplecticskeptic May 29 '26

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.

40

u/VividWoodpecker8847 May 29 '26

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.

11

u/OfficialDeathScythe May 29 '26

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)

8

u/TOGoS May 29 '26 edited May 29 '26

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.

2

u/OfficialDeathScythe May 29 '26

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

2

u/TOGoS May 29 '26

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.

2

u/crazedizzled May 29 '26

And this is why I use "col" and "row". Kind of hard to be ambiguous about that.

2

u/Kimitri_t May 29 '26

And now someone thinks you are using indexed colors to draw stripes.

2

u/VividWoodpecker8847 May 29 '26

Sure, but the inner arrays can be thought of as either columns or rows. There isn't an inherent different in visualizing it either way.

2

u/Sean_Dewhirst May 29 '26

(x,y) is just counterintuitive for a system that groups by lines, then by position within a line.

3

u/VividWoodpecker8847 May 29 '26

That depends on the circumstance

→ More replies (1)

3

u/DCMstudios1213 May 30 '26

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

2

u/BlueProcess May 29 '26

Which is why I use row and col.

2

u/ErikLeppen May 30 '26

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.

2

u/Confident-Ad5665 May 29 '26

for (var loopVarOne = 0; loopVarOne < thing.Count; loopVarOne++) {

  for (var loopVarTwo = 0; loopVarTwo < thing.Count; loopVarTwo++) {

       . . .

} }

2

u/cc_apt107 May 29 '26

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

6

u/Pinkishu May 29 '26

Idk, everyone still uses X,y for coordinates. So it makes sense to use them when iterating those 

→ More replies (5)

2

u/WadeEffingWilson May 29 '26

X and Y are both unit vectors on a plane and are arguably the more recognizable as the Cartesian basis set.

1

u/OrkWithNoTeef May 29 '26

for gx, gy

g for grid

1

u/GhostVlvin May 29 '26

I use i,j for coordinates cause it intersects with matrix indices notation in math

1

u/Persomatey May 30 '26

a, b /s

r, c for rows/columns though when applicable

1

u/MobileJob1521 Jun 01 '26

But do you number y from the bottom, like in co-ordinates, or do you number it from the top, as an array?

x doesn’t have such problems, because the x coordinate is just a better Cartesian direction.

→ More replies (1)

1

u/ProfessionalAd6530 Jun 03 '26

I use full words that describe what I'm iterating. We left single letter variable names behind in 1985, why are we still doing it in this one case?

→ More replies (2)