3 ms·
> I don’t need a keyword just to tell me that a variable isn’t modified during its lifetime. I agree that this isn't very well argued, and variable re-use is a
by thamer 5y ago
> I don’t need a keyword just to tell me that a variable isn’t modified during its lifetime.
I agree that this isn't very well argued, and variable re-use is a common source of bugs – especially variables that are used several times in a single function for different purposes.
On a similar point, Java has `final` instead of `const` and it does apply to local variables, including to guarantee that a variable is not only not modified, but set exactly once. Something like this:
final String foo;
if (condition) {
foo = someValue();
} else {
foo = otherValue();
}
Omitting any of the assignments leads to a compilation error. It works with complex if conditions with multiple branches, and even with `switch`. If a branch throws an exception, that's also acceptable as the variable is guaranteed not to be used below.
I find it pretty useful; doesn't C++ provide a similar construct?
- rightbyte 5y ago> I find it pretty useful; doesn't C++ provide a similar construct? static const?