Learning and understand the uses of the const keyword

Using const is another area of programming that seems to be a little controversial. Some programmers argue that they have never had a bug where using const would have helped. Others feel that, since you can't guarantee that a const object won't be modified, it is completely worthless. The fact is that const objects can be modified. const is not magic. So, is const correctness still a good thing? Before we get into that, let's have a look at what const is.

When you create a const variable, you must initialize it. All const variables will be checked at compile time to make sure that the variable is never assigned a new value. Since it happens at compile time, it doesn't have an influence on the performance. These are a few benefits that we should consider. First, it improves readability. By marking a variable as const, you are letting the reader know that this variable is not supposed to change. You are sharing your intent about the variable and making your code self-documenting. const variables are also usually named in all capital letters, which further helps with readability. Second, since the variable is checked at compile time, there is no way for the user to accidently change the value. If someone tries to modify the variable, it will cause a compiler error. This is great for you if you were expecting the value to stay the same. This is great for the user if the modification was truly an accident.

Compiler errors should always be preferred over runtime. Anytime we can use the compiler to help us find problems, we should. This is the same reason that many programmers choose to set their compiler warnings to maximum and treat those warnings as errors. Time spent fixing a known compiler issue is time you won't have to spend finding the runtime error that it might cause.

Additionally, const variables should be preferred over C style #define macros. Macros are a blunt tool. Sometimes they may be the only tool for the job, but they are overkill for simple symbolic constants. Macros do a blind find and replace. Anywhere the symbolic constant is in the source code, the value will replace it. While these situations may be rare, they can also be frustrating. Since the values are replaced in the pre-processing phase, the source code is unchanged when you go to fix the problem.

The const variables, on the other hand, are part of the language. They follow all the normal language rules for types and operators. There is nothing mysterious happening. They are just variables that can't be reassigned:

int i1;            //No initialization, OK 
int i2 = 0; //Initialization, OK

const int ci1; //ERROR: No initialization
const int ci2 = 0; //Initialization, OK

i1 = 10; //Assignment, OK
i2 += 2; //Assignment, OK

ci1 = 10; //ERROR: Can't Assign
ci2 += 2; //ERROR: Can't Assign
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.185.196