声明正确的变量类型有多重要

发布于 2024-12-06 15:05:23 字数 181 浏览 2 评论 0原文

如果我只有 1 和 1 之间的数字10 将其声明为 int 是否太过分了,还是应该使用 shortLongsbyte

int x = 5;sbyte x = 5;

If I have a number only between 1 & 10 is it overkill declare it as int or should you use short, Long, sbyte?

int x = 5; or sbyte x = 5;

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

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

发布评论

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

评论(6

歌入人心 2024-12-13 15:05:23

int 在 32 位处理器上处理得更快,因为它的大小等于 CPU 寄存器的大小。如果您没有其他要求(例如需要节省内存),请使用 int

int is handled faster on 32 bit processors, since its size is equal to CPU register size. If you don't have additional requirements (the need to conserve memory, for example), use int.

花之痕靓丽 2024-12-13 15:05:23

如果没有更多上下文,我会说只使用 int 。仅当空间有限或使您想要做的事情过于复杂时,过度杀伤才有意义。

Without more context I would say just use an int. Overkill only matters when you have space constraints or it overly complicates what you're trying to do.

私藏温柔 2024-12-13 15:05:23

事实并非如此,除非您想从应用程序中获得极少量的性能和内存占用。

但是,如果不是出于可读性目的,我建议使用正确的类型,并且如果该值增加到超过其分配的类型大小,那么这个潜在的错误将在编译时被捕获。

Not really unless your are tying to get minuscule amounts of performance and memory footprint out of your application.

However I would suggest using the correct type if not for readability purposes and also so that if the value increases above it's allocated type size then this potential bug will be caught at compile time.

森林迷了鹿 2024-12-13 15:05:23

代码的简单性是长期可维护性的一个重要特征。除非优化至关重要,例如在超高速应用程序中(即时反应至关重要的游戏),否则不要浪费时间担心几个字节,因为这会让下一个人更难理解。

Simplicity in your code is an important feature for long term maintainability. Unless optimization is vitally important such as in super high speed applications (games where instant reactions are critical), don't waste time worrying over a few bytes that would make it harder for the next guy to understand.

桃气十足 2024-12-13 15:05:23

如有疑问,请勿优化。在代码库的早期阶段进行过度优化通常是一个坏主意,它会导致抽象泄漏、代码库的通用性差以及难以调试问题。坚持使用类型系统中最常见的类型,每当您发现类型只需为 shortuint 时,您可以随时更改类型。

与相反的方式相比,从广泛开始并缩小类型更容易,并且在拥有与第三方系统等集成的工作代码库之前,不可能确切地知道代码中需要在哪里绘制边界。

When in doubt, don't optimize. Over optimization in the early stages of a code base is usually a bad idea and leads to abstraction leakage, bad versatility of the code base and hard to debug problems. Stick with the most common types in the type system and whenever you discover that the type only needs to be short or uint, you can always change type later on.

It's easier to start out wide and narrow the types down than the other way around and before you have a working code base that integrates with 3rd party systems and such, it's impossible to know exactly where the boundaries in your code needs to be drawn.

新人笑 2024-12-13 15:05:23

称狗为狗,称推销员为推销员。以正确的类型声明变量是代码可读性和准确性的基础。

现在,对于您的示例,如果您确定它只是从 1 到 10,则可以使用 sByte。不过,我会使用int。只要有可能,我们就不应该过于复杂化。

另外,正如 @ElRonnoco 所注意到的,如果您计划执行计算,则在处理小数字类型时可能会出现溢出错误,因此对于大多数情况,您应该坚持使用 int

Call a dog a dog and a salesman a salesman. Declaring your variables in the right type is fundamental for code readability and accuracy.

Now, for your example, if you are sure it's only from one to 10, you can use sByte. I would, however, use int. We should not overcomplicate, whenever we can.

Also, as well noticed by @ElRonnoco, if you are planning to execute calculations, you might get an overflow error when dealing with small numeric types so you should stick to int for most things.

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