How often I should be using const? Is it useless/overrated?
Kagamin
spam at here.lot
Tue Nov 22 11:19:18 UTC 2022
On Friday, 18 November 2022 at 17:57:25 UTC, H. S. Teoh wrote:
> You're looking at it the wrong way. The kind of issues having
> const
> would solve is like when your function takes parameters x, y,
> z, and
> somewhere deep in the function you see the expression `x +
> y*z`. If x,
> y, and z are const, then you immediately know what the value of
> this
> expression is. However, if they were not, then you'd have to
> trace
> through all of the preceding code to figure out whether their
> values
> have changed, and how they have changed. The former makes the
> code
> easier to understand, the latter adds complexity to
> understanding the
> code.
AFAIK Rust allows shadowing (intentionally) to solve usability
problems with immutable variables, so when deep in the function
you see `x+y*z`, you can't immediately tell its value, because
the variables could be previously shadowed and you have to trace
through all of the preceding code to figure it out :)
More information about the Digitalmars-d-learn
mailing list