Click here to Skip to main content
15,883,886 members
Articles / Programming Languages / C++

To Const or Not to Const

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
14 Jan 2020MIT5 min read 7K   6   5
const-correctness, about when and why you should use const keyword - this post was inspired by a rule from the first chapter of Code Craft, by Pete Goodliffe, on Defensive programming

In this article, we are going to talk about const-correctness, about when and why you should use const keyword. This post was inspired by a rule from the first chapter of Code Craft, by Pete Goodliffe, on Defensive programming.

The Const-Correctness Principle

To define what const-correctness is, we must first define what const is. To quote Wikipedia on the subject:

const is a type qualifier: a keyword applied to a data type that indicates that the data is read only.

This sums up well what const is. It also can have some specific meaning depending on the languages or the type it qualifies, but we will talk about it later.

Now that the definition of const is well established, let’s take a look at what const-correctness is :

const-correctness is the rule which specify that all variables or objects should be declared with the const keyword unless they need to be modified.

Why Is It Important ?

So ok, we have the definition, but you may wonder why such a rule? Why should we care? Why is it even mentioned in the first chapter of Code Craft, by Pete Goodliffe? 🤔

First of all, using const whenever we can will make our code easier to understand, and to reason about. Indeed, if you know that a variable is qualified with const, then, you don’t have to think that it may have been modified. It can also allow you to specify what are the inputs of a function by forcing them to be Read-only, and just like that, you give information about the value’s intended use.

This may not seem a lot, but just like that, this rule will increase the readability and comprehensibility of code and makes working in teams and maintaining code simpler. Moreover, this rule can also help the compilers and the optimizers when reasoning about code and optimize it better. 💪

Applying the const-correctness is also a good way to start defensive programming in a project. It’s simple (only 5 characters to add in some places), and your code will start to make more sense, to keep more of the author intentions.

For all of those reasons, you should definitely adopt this rule.

Examples in C++

In C++, you can put the keyword const in a lot of places!

Warning: I don’t care about the east const VS west const debate. I find it childish to be honest 😝, but you can talk about it in the comments, if you really want to.

So, I was saying that in C++, you can put the keyword const in a lot of places! And I won’t talk about them all, there are actually too many cases. Instead, I will focus on the meaning that the const keyword can give, the intent that you can give to an element with it. But if you want to know it all, I encourage you to look on the ISO cpp website.

The first case I want to talk about with you is when const qualifies a variable. Depending on the purpose of the variable, the const keyword can reflect different intent from the author. 🙂

For example, if an attribute of a class has the keyword const, then it means that the author wants it to be either set only by a constructor as always the same for all the instance of a class, in which case, he may prefer using the keywords static and constexpr, but the use of constexpr can be another article all by itself, so we will only mention it here. 😉

C++
class MyClass
{
public:
    MyClass(int value) : my_constant(value) {}

private:
    const int my_constant{0};
    static constexpr int my_other_constant{42};
}

Another case could be to get the return variable of a function/method. In that case, the const will make sure that the result of the function/method won’t be accidentally modified, which can be handy if the content of the variable is an error code, for example. 👍

C++
const auto error_code = methodReturningAnError();

The final example on the variables that we are going to address is when you combine the const keywork to the & for either a parameter of a function/method, or when getting the result of a function/method, which allows the compiler to avoid copy and to make sure that the variable won’t be modified:

C++
const auto& variable = methodReturningAnHugeElement();

Since this "trick" allows you to avoid unnecessary copy, it can be really interesting to use it whenever you can so that your program will be more understandable, faster and your intent clearer for any other programmer that is going to read your code in the future. 💪

And finally, to finish the examples on C++, I want to talk about the use of the const keyword in the method prototype. It allows the method to specify that it won’t modify any attribute of the class and that all the functions called in it will do the same. It may not seem like much, but it is a huge indication for the next programmer who is going to read this code, and be a guard for any programmer that is going to modify the code of this function or of any function called by it.

C++
auto myMethod (auto parameter) const;

And voilà, this is it for the intent of the const keyword that I wanted to talk about in C++. If you have any other, please tell me in the comments. 😉

Conclusion

const-correctness is a simple but powerful rule that you should definitely use, whatever language you are using (if it defined a const keyword). It will help you add some defence to your code, and clarify your intention for the next developer to pass. And finally, it may even help compilers and optimizers do a better job.

Surely, you will have a code with a lot of const all over the place, but it will only reflect the logic of your code and the intention of developers try to communicate to you their intention to have a safe and persistent code.

Thank you for reading this article. And until my next article, have an splendid day! 😉

Extra Resources

License

This article, along with any associated source code and files, is licensed under The MIT License



Comments and Discussions

 
Questionconst in class Pin
geoyar13-Jan-20 14:18
professionalgeoyar13-Jan-20 14:18 
AnswerRe: const in class Pin
10xlearner14-Jan-20 3:52
10xlearner14-Jan-20 3:52 
Generalconst is a 5 letter word, not 4 Pin
Shao Voon Wong10-Jan-20 16:34
mvaShao Voon Wong10-Jan-20 16:34 
GeneralRe: const is a 5 letter word, not 4 Pin
10xlearner14-Jan-20 3:52
10xlearner14-Jan-20 3:52 
SuggestionRe: const is a 5 letter word, not 4 Pin
Chad3F23-Jan-20 14:38
Chad3F23-Jan-20 14:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.