Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it worth to insert `const`-correctness

I'm currently confronted with a C++ project written by some senior programmers consisting of about 400 files and 200 classes.

The code is well elaborated, works fine and stable.

While I'm adding some features, for me it is just ordinary practice to take care about const-correctness.

But if I start to declare my new member functions const, there is no end with adapting old code in order make things work.

  • Should I invest the amount of time to introduce const-correctness into this code?
  • Even worse, I have to touch and alter the old mature code and explain to the seniors what I did during the code review. Is it worth it?
like image 650
Maus Avatar asked Sep 29 '11 09:09

Maus


People also ask

Is const correctness important?

The benefit of const correctness is that it prevents you from inadvertently modifying something you didn't expect would be modified.

Does using const improve performance?

const correctness can't improve performance because const_cast and mutable are in the language, and allow code to conformingly break the rules. This gets even worse in C++11, where your const data may e.g. be a pointer to a std::atomic , meaning the compiler has to respect changes made by other threads.

Does C have const correctness?

In C, C++, and D, all data types, including those defined by the user, can be declared const , and const-correctness dictates that all variables or objects should be declared as such unless they need to be modified.

What is a situation in which returning a const value is good idea?

In the hypothetical situation where you could perform a potentially expensive non-const operation on an object, returning by const-value prevents you from accidentally calling this operation on a temporary.


2 Answers

Const correctness is a kind of additional layer over static typing that is meant to make it easier to develop mature, reliable and robust code. You say that you already have the latter. In that case, enforcing const correctness in such codebase doesn't seem to have significant, added value from the pragmatic point of view.

like image 87
Xion Avatar answered Nov 15 '22 20:11

Xion


Should I invest the amount of time to introduce const-correctness into this code?

If you feel that you can get it all done in a reasonable time, sure. const-correctness is a good thing, so if you can adjust the codebase to employ it properly then that can never be bad.

It all comes down to how much time you have available and what else you could be doing instead, which is more about project management and more appropriate on programmers.SE.

Even worse, I have to touch and alter the old mature code and explain to the seniors what I did during the code review. Is it worth it?

It's certainly worth it for them (and, by extension, to everybody else). It sounds like they will learn a lot in code review, which is fantastic!


Edit

As molbdnilo rightly points out, this is a big change and you should definitely have a group discussion about it before starting. This would be more appropriate than leaving it to code review in two weeks' time, when you've already done it.

like image 30
Lightness Races in Orbit Avatar answered Nov 15 '22 21:11

Lightness Races in Orbit