-
November 4th, 2009, 01:01 PM
#1
Question regarding "const"
Ok this may be a stupid questions however, the book I am studying from says that following the principles of least privilege you should always declare a variable (arrays and such) const if the function is not going to manipulate the data. IE: just printing out and array or something similar. My question is what difference does it make weather I do or not. If I don't code the function to change the value then why is const so important and stressed so much as good eng practice. Thanks in advance for your replies.
-
November 4th, 2009, 01:08 PM
#2
Re: Question regarding "const"
Because the compiler will report an error if your code accidentally manipulates data that it shouldn't. Using the const qualifier makes it very difficult to accidentally modify data or pass the const data to functions that take non-const pointers or references. By using the const qualifier you are helping the compiler which in turn will help you find errors before you run!
-
November 4th, 2009, 01:09 PM
#3
Re: Question regarding "const"
Originally Posted by robwong81
If I don't code the function to change the value then why is const so important and stressed so much as good eng practice.
What if you, or someone who goes on to maintain your code, changes the function by accident such that it does change that value, but elsewhere you rely on the assumption that it does not?
-
November 4th, 2009, 01:11 PM
#4
Re: Question regarding "const"
Let's say you're working with another engineer. He's being const-correct, and you aren't. Then his function (which takes a const array as a parameter) calls your function to print it out---but he gets a compile error, because he can't pass his const array to your function expecting a non-const array.
That is the basic problem: As soon as you start using const a little bit, you have to use it everywhere, or you run into trouble. You can't do things in half measures.
So why use it at all? Well, why not. Using const has several advantages:
1) It's a form of self-documentation, letting users of the code (who aren't you) know something about the semantics.
2) It's an easy way to increase compile-time checking. You always want your errors to be caught at compile time rather than runtime if at all possible, and const is a trivial means of helping out with that.
3) In a few very specific cases, it can help the compiler do a better job of optimizing your code. Since the programmer is able to bypass const-ness, this isn't as big a factor as it could be, however.
-
November 4th, 2009, 01:37 PM
#5
Re: Question regarding "const"
Ok so then it seems it has more of a structured usage than an actual needed use. Its main purpose is for maintaining code and working with others who use it in a big project that your a part of. Thanks to all of you for your quick and discerning answers.
-
November 4th, 2009, 02:14 PM
#6
Re: Question regarding "const"
Yeah, it's there to make your life easier, but the code won't run any differently without it.
-
November 5th, 2009, 03:29 AM
#7
Re: Question regarding "const"
Originally Posted by robwong81
Ok so then it seems it has more of a structured usage than an actual needed use. Its main purpose is for maintaining code and working with others who use it in a big project that your a part of. Thanks to all of you for your quick and discerning answers.
Not 100% true. If you are coding on an embedded environment, then your const objects may not be in the same address space as your non-const objects. Also, using const may help your compiler create create more efficient code:
Code:
const int a = 5;
doSomething(a);
print(a);
Look at the above code. Your compiler will be smart enough to know that a will be 5 throughout the program, and won't bother "checking if it really is". If a was not const, your compiler would have to check a after doSomething, just in case doSomething changed a.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|