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.
const
-correctness
into this code?The benefit of const correctness is that it prevents you from inadvertently modifying something you didn't expect would be modified.
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.
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.
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.
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.
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With