为什么在编程中使用的常量? [英] Why use constants in programming?

查看:138
本文介绍了为什么在编程中使用的常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚回去过了一下使用艾弗霍顿的开始本C语言书中Ç学习。我到了一些关于声明的常量,这似乎混合起来,在同一个句子中的变量。

I've just been going back over a bit of C studying using Ivor Horton's Beginning C book. I got to the bit about declaring constants which seems to get mixed up with variables in the same sentence.

只是为了澄清,是在用C指定常量和变量的区别,真的,当你需要使用一个常量,而不是一个变量?我知道人说使用恒定的,当信息程序执行过程中不会改变,但我真的不能相信的时候,一个变量不能被使用。

Just to clarify, what is the difference in specifying constants and variables in C, and really, when do you need to use a constant instead of a variable? I know folks say to use a constant when the information doesn't change during program execution but I can't really think of a time when a variable couldn't be used instead.

推荐答案

一个变量,你可以从名字猜测,随时间变化。如果它不发生变化,有无损失。当你告诉该值将不会更改编译器,编译器可以做一大堆的优化,如内联直接的价值,从来没有分配堆栈上不断的任何空间。

A variable, as you can guess from the name, varies over time. If it doesn't vary, there is "no loss". When you tell the compiler that the value will not change, the compiler can do a whole bunch of optimizations, like directly inlining the value and never allocating any space for the constant on the stack.

但是,你不能总是在你的编译器计数足够聪明,能够正确地判断一个值将更改设置一次。当编译器不能确定这跟100%的信心在任何情况下,编译器会犯错安全方面,并假定它可能会改变。这可能会导致像内联回避,不优化某些循环各项性能的影响,创建对象code,它并不像并行友好。

However, you cannot always count on your compiler to be smart enough to be able to correctly determine if a value will change once set. In any situation where the compiler is incapable of determining this with 100% confidence, the compiler will err on the side of safety and assume it could change. This can result in various performance impacts like avoiding inlining, not optimizing certain loops, creating object code that is not as parallelism-friendly.

正因为如此,而且由于可读性也很重要,你要努力使用显式恒尽可能离开变量的东西,可以真正改变。

Because of this, and since readability is also important, you should strive to use an explicit constant whenever possible and leave variables for things that can actually change.

至于为何常数代替面值数字:

As to why constants are used instead of literal numbers:

1),它使code更具可读性。每个人都知道什么是3.14(希望),不是每个人都知道3.07是PA的所得税税率。这是特定领域的知识的例子,不是每个人都保持你的code在未来(例如,个税软件)都会知道。

1) It makes code more readable. Everyone knows what 3.14 is (hopefully), not everyone knows that 3.07 is the income tax rate in PA. This is an example of domain-specific knowledge, and not everyone maintaining your code in the future (e.g., a tax software) will know it.

2),当你做出改变保存工作。走出去,改变每3.07 3.18,如果在未来税率变化将是烦人。你总是希望尽量减少变化,最好做一个改变。你必须做出更多的并发变化,你会忘了什么东西,导致失误的风险就越高。

2) It saves work when you make a change. Going and changing every 3.07 to 3.18 if the tax rate changes in the future will be annoying. You always want to minimize changes and ideally make a single change. The more concurrent changes you have to make, the higher the risk that you will forget something, leading to errors.

3)你避免那些危险的错误。试想一下,有两个国家拥有3.05的所得税税率,然后其中一人在3.07变为3.18,而其他的客人。通过正要和更换,可以用严重错误告终。当然,许多整数或字符串常量的值是除3.07更常见。例如,数字7可以重新present天的中周数,以及别的东西。在大型项目,这是非常难以确定每一个字面值手段。

3) You avoid risky errors. Imagine that there were two states with an income tax rate of 3.05, and then one of them changes to 3.18 while the other stays at 3.07. By just going and replacing, you could end up with severe errors. Of course, many integer or string constant values are more common than "3.07". For example, the number 7 could represent the number of days in the week, and something else. In large programs, it is very difficult to determine what each literal value means.

4)在串的文本的情况下,它是通常使用的符号的名称的字符串,以允许串池在支持多语言的情况下快速地改变。

4) In the case of string text, it is common to use symbolic names for strings to allow the string pools to change quickly in the case of supporting multiple languages.

请注意,除了变量和常变量,也有一些种语言枚举。枚举实际上允许你定义一个类型为一小群常数(例如,返回值),所以使用起来会提供类型安全。

Note that in addition to variables and "constant variables", there are also some languages with enumerations. An enumeration actually allows you to defines a type for a small group of constants (e.g., return values), so using them will provide type safety.

例如,如果我有一个枚举数周的天,换了个,我会,如果我一个月指定到一天警告说。如果我只是用整型常量,也不会有警告在对3一天分配到一个月3.您总是想类型安全,而且提高了可读性。枚举也用于定义以便更好。试想一下,你有一周的日子常数,现在你想你的那一周开始在周一而不是周日。

For example, if I have an enumeration for the days of the weeks and for the months, I will be warned if I assign a month into a day. If I just use integer constants, there will be no warning when day 3 is assigned to month 3. You always want type safety, and it improves readability. Enumerations are also better for defining order. Imagine that you have constants for the days of the week, and now you want your week to start on Monday rather than Sunday.

这篇关于为什么在编程中使用的常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆