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

发布于 2024-08-14 17:51:23 字数 474 浏览 5 评论 0原文

我的代码中有一些特定的位置,我使用特定的像素尺寸将某些内容传输到屏幕上。显然,这些被放置在命名良好的常量中,但我担心它仍然有点模糊。

示例:这是在一个小函数的局部范围内,所以我希望很明显常量的名称适用于方法名称所指的内容。

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

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

屏幕上的位置是专门为该地点计算的。我几乎觉得我应该制作 SCREEN_BOTTOM_RIGHT 常量,这样我就可以做类似 const int X_COORD = SCREEN_BOTTOM_RIGHT - SOME_OTHER_NAME 的事情。

上面的代码是不是太含糊了?或者作为开发人员,您会看到这一点并说,好吧,屏幕上就是 (430, 458)。知道了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

标点 2024-08-21 17:51:23

视情况而定。这些常数之所以如此,有什么特殊原因吗? (例如,“430”实际上是某个其他元素左侧的 200 像素吗?)

如果是这样,那么用用于其他元素的常量(或任何原因导致的结果)来表达它可能更有意义在那个数字中)。

如果它们都是任意位置,那么将它们表示为坐标是有意义的。但很可能,它们实际上并不是任意的。

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.

§普罗旺斯的薰衣草 2024-08-21 17:51:23

您假设我有多大尺寸的屏幕?人们的机器上的屏幕分辨率非常不同,任何固定的像素大小或位置有时对某些人来说都是错误的。我的正常显示是1900x1220;我的另一个显示器是 1440x1050;其他人使用不同尺寸的屏幕。如果您要显示用户无法调整大小的固定大小窗口,那么使用固定大小可能更安全。

在不知道ApplySurface()的作用的情况下,很难说它是否如所写的那样清楚。然而,相对名称很可能是明智的。作为一名维护程序员,如果没有支持注释,我不知道值 430 和 458 是从哪里派生的,除非您使用表达式来明确说明。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文