Why would you use the keyword const if you already

2019-02-02 01:50发布

Many of the books that I am reading use keyword const when the value of a variable should not be modified. Apart from specifying to readers of the code that you may cause errors if you modify this variable (you can use comments to do this), why would you need that keyword to be a part of any programming language? It seems to me that if you don't want a variable modified, simply don't.

Could someone clarify this for me?

11条回答
Explosion°爆炸
2楼-- · 2019-02-02 02:26

At least in C++, const has some uses beyond just documenting your intent to other programmers.

const can also tell the compiler some things. For example, a function that takes a reference, like: void f(T &t); can't accept a temporary object as its parameter. To get it to do so, you need to const qualify the reference, like: void f(T const &t).

Likewise, to invoke a member function on a const object, the member function must be const qualified like: void T::foo() const {}.

In an embedded system, const can mean more still, possibly telling the compiler about where to locate the object in question (putting it in ROM vs. RAM). const by itself isn't necessarily enough tell it "put this object in ROM", but it's still often a prerequisite.

Likewise (under C++11) const tells the compiler about thread safety.

Now, it's undoubtedly true that you could define some other language that (in other ways) bore some resemblance to C or C++ that didn't use const in these ways. The result would be a rather different language from either one though. Without knowing your intent, it's impossible to say how it would turn out, but it might end up closer to Java or C# (for a couple of examples) both of which are somewhat similar to C and C++ in some ways, but not this particular one (i.e., don't use const like C and C++ do).

查看更多
\"骚年 ilove
3楼-- · 2019-02-02 02:30

Consider the scenario where you are using the same constants many times in your entire project and you are hard coding it at all places. Now suddenly you are required to change the value of constant to another value, so it will be hectic to do changes at all the locations.

So I think to make your code more maintainable definitely be one of the reason.

查看更多
smile是对你的礼貌
4楼-- · 2019-02-02 02:34

Two reasons:

  1. compiler enforced documentation
  2. compiler optimizations

Here is a good explanation from Ian Lance Taylor (who worked on gcc and gold linker):

The first meaning of const has a real effect on the program. A variable declared const may be compiled differently from a variable which is not declared const.

The second meaning of const, on the other hand, is, in effect, compiler-enforced documentation. The compiler will issue an error if an attempt is made to change a value using a const qualified pointer, but declaring such a pointer will not change the generated code.

查看更多
Emotional °昔
5楼-- · 2019-02-02 02:34

Constant variables just allow you to write more readable code.

The most use for const in almost all languages is to allow us to use names to refer to constants values, so you can tell to others, using fluent language, what this name refers to without needing to spread comments in your code and saving time and effort from the readers to know the parameters type and particularities of the parameters. Of course, you benefit also if your constant value is reused across your code. Well, a code like this can be more readable:

processPages(LETTER_PAPER_WIDTH, LETTER_PAPER_HEIGHT);

...than this:

processPages(215.9, 279.4); // 8.5 x 11 Inches in millimeters for Letter Papers

In the above example, you need to understand what each parameter is, its unit and the type to interpret the values, and you need yet to validate it against the comment, because a redundant comment like this (those comments that replay what is coded) isn't a reliable and so useful comment (is a bad comment acording to Robert Martin in Clean Code: http://goo.gl/5EyY).

查看更多
We Are One
6楼-- · 2019-02-02 02:36

Besides the usual programming considerations already discussed in other answers, one thing that concerns me is the attitude:

It seems to me that if you don't want a variable modified, simply don't.

A general rule of thumb is that 20% of the cost of writing code is spent in the development phase. The other 80% is spent over the lifetime of the code upgrading it, maintaining it, etc. This means that many other people will work on your code other than yourself.

Time spent during development that avoids problems years later is a good investment. This effort includes: writing comments; defining constants that are constants; and writing explicit code that does not rely on obscure language constructs.

Also, @worlboss, I hear a fair amount of intolerance. As some others have commented, carbon units make mistakes and anything a silicon unit can do to help avoid mistakes is appreciated.

查看更多
登录 后发表回答