简体   繁体   English

是否应该重构包含幻数的屏幕尺寸常数?

[英]Should screen dimension constants that hold magic numbers be refactored?

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 . 我几乎感觉好像我应该制作一个表示SCREEN_BOTTOM_RIGHT常量,这样我就可以像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. 或作为开发人员,您会看到并在屏幕上说(430,458)。 Got it. 得到它了。

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?) (例如,“ 430”实际上是其他元素左侧的200个像素吗?)

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. 但是机会是,它们实际上并不是任意的。

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; 我的正常显示是1900x1220; my other display is 1440x1050; 我的其他显示器是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. 不知道ApplySurface()的作用,很难说它是否清楚如写。 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. 作为维护程序员,除非您使用表达式将其弄清楚,否则我不知道值430和458是在没有支持注释的情况下派生的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM