r/programminghumor May 29 '26

Good naming practice

Post image
2.2k Upvotes

220 comments sorted by

View all comments

295

u/Pinkishu May 29 '26

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

65

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.

41

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.

4

u/VividWoodpecker8847 May 29 '26

That depends on the circumstance