Using const
is another area of programming that seems to be a little controversial. Some programmers argue that they have never had a bug where using const
would have helped. Others feel that, since you can't guarantee that a const
object won't be modified, it is completely worthless. The fact is that const
objects can be modified. const
is not magic. So, is const
correctness still a good thing? Before we get into that, let's have a look at what const
is.
When you create a const
variable, you must initialize it. All const
variables will be checked at compile time to make sure that the variable is never assigned a new value. Since it happens at compile time, it doesn't have an influence on the performance. These are a few benefits that we should consider. First, it improves readability. By marking a variable as const
, you are letting the reader know that this variable is not supposed to change. You are sharing your intent about the variable...