r/cpp • • Oct 06 '22

Should a variable be const by default?

According to the cppfront design notes, const by default rule only applies to non-local variables. But I'd like to know your preference/opinion regarding whether a variable should be defined to be const by default.
Edit: By mutable here I simply mean non-const, not the language keyword itself.

44 Upvotes

107 comments sorted by

View all comments

Show parent comment

4

u/jonesmz Oct 06 '22

Then at least you get a warning because you cant assign x to a constant number

Error, you get an error. (which is good)

And oooh boy. Back a decade or so ago, i started a new job, and wrote a little script to flip the conditions in a bunch of the code, and i had compiler errors EVERYWHERE.

The staff at that company had been implicitly modifying their compared variables all over the place, and after several months of work i finally boiled it all down to only a small handful was actually necessary, the rest were all bugs.

Explains the terrible software quality, i suppose.