views:

75

answers:

2

I have a few specific places in my code where I use specific pixel dimensions to blit certain things to the screen. Obviously these are placed in well named constants, but I'm worried that it's still kind of vague.

Example: This is in a small function's local scope, so I would hope it's obvious that the constant's name applies to what the method name refers to.

const int X_COORD = 430.0;
const int Y_COORD = 458.0;

ApplySurface( X_COORD, Y_COORD, .... );
...

The location on the screen was calculated specifically for that spot. I almost feel as if I should be making constants that say SCREEN_BOTTOM_RIGHT so I could do like something like const int X_COORD = SCREEN_BOTTOM_RIGHT - SOME_OTHER_NAME.

Is the code above too ambiguous? Or as a developer would you see that and say, alright, thats (430, 458) on the screen. Got it.

+1  A: 

Depends. Is there a particular reason those constants are what they are? (For instance, is that "430" actually 200 pixels to the left of some other element?)

If so, then it'd probably make more sense to express it in terms of the constant used for the other element (or whatever reason results in that number).

If they're all just arbitrary positions, then expressing them as coordinates makes sense. But chances are, they're not actually arbitrary.

Amber
Yes, I have a namespace that holds the dimensions of the screen, and I considered doing something like Screen::RIGHT_SIDE - number. I guess that's the way to go.
Anonymous
A: 

What size screen are you assuming I have? People have very different screen resolutions on their machines, and any fixed pixel size or position is going to be wrong for some people some of the time. My normal display is 1900x1220; my other display is 1440x1050; other people use screens with different sizes. If you are displaying to a fixed size window that the user cannot resize, then it may be safer to use fixed sizes.

Without knowing what ApplySurface() does, it is hard to say whether it is clear as written. However, the relative names could well be sensible. As a maintenance programmer, I'd have no idea where the values 430 and 458 were derived from without a supporting comment unless you used an expression to make it clear.

Jonathan Leffler