1: The point of const to me is to let the user know that the original author INTENDED it to be immutable.
-
-
2: By saying "always use const and change it whenever it happens to be mutated", you're losing an important signal:
1 reply 0 retweets 0 likes -
3: That the original author considered it questionable to mutate the variable.
1 reply 0 retweets 0 likes -
4: And what are we gaining? In exchange for losing user intent, we gain a minor but noisy piece of metadata ...
1 reply 0 retweets 0 likes -
5: that is trivial to see by scanning the code (is it mutated is the same as "is there an x= somewhere in the function?")
1 reply 0 retweets 0 likes -
6: If your functions are too large for it to be quickly analyzable, consider smaller functions.
1 reply 0 retweets 0 likes -
7/7: Your code will be clearer and you won't have given up an important source of user intent.
1 reply 0 retweets 0 likes -
I’m guilty of using “const” everywhere I can out of habit, but I doubt it has really ever prevented a bug.
1 reply 0 retweets 2 likes -
It also devalues the meaning of const when you wrote it for a reason ("don't change this module-level variable plz")
1 reply 0 retweets 0 likes -
In rust that kind of change is a public API-breaking change so people are more intentional about it.
1 reply 0 retweets 0 likes
I really want const to mean "the author thought this variable shouldn't be mutated" not "it happened to not be mutated".
-
-
I can tell the latter by looking at the code. The former is lost forever.
0 replies 0 retweets 0 likesThanks. Twitter will use this to make your timeline better. UndoUndo
-
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.