does it use less memory or can it access the values faster?
Usually neither. It just makes the program more robust because you (or other people) cannot change the value accidentally.
This makes especially sense in public APIs when you want to show consumers that you won’t modify their values. Every modification of a variable means a change in the program state. In order for programmers to be sure that their program works correctly they need to keep track of the state, and this is tremendously more difficult if they don’t know when or how their variables are changed.
The primary purpose of const
is therefore documentation of semantics and this is a very powerful purpose. Use const
often.