C++ Programming Practices – Should const Be Used More?

ccoding-standardscoding-styleconstprogramming practices

There are some things I am pretty strict about, but const has not been one of them.

For example, I might make a local variable that I use three times in a function, that does not get changed, and yet I do not bother making it const.

Is this something that I should be more strict about? Are there any tangible benefits that I should be aware of? Is this something you would flag if you were reviewing my code?

Best Answer

Is this something that I should be more strict about? Are there any tangible benefits that I should be aware of?

As @JerryHeremiah points out, after some point it doesn't add anything for compiler optimization, but it can improve readability.

I tend to do this (use const when possible, for extra readability), because it fits the Law of Demeter ("if this value doesn't need to be modified, then do not allow it to be, past this point") and expresses the author's intent better (in a way the compiler checks for).

Is this something you would flag if you were reviewing my code?

Depends on the agreed-upon review code criteria for a project (yes, it is something I would flag, if this were a condition to be flagged for the coding standards of the project, and I would also argue that this should be a condition to be flagged for a project).