←back to thread

498 points azhenley | 6 comments | | HN request time: 1.121s | source | bottom
1. loeg ◴[] No.45768135[source]
Yeah, I also wish it was the default. But it's a little too verbose to just sprinkle on every variable in C++. Alas. Rust gets this right, but I'm stuck with C++ at work.
replies(2): >>45771522 #>>45773574 #
2. veltas ◴[] No.45771522[source]
100%, life's too short.

Ultra-pedantic const-correctness (vs tasteful const-correctness on e.g. pass-by-reference arguments or static/big objects) catches nearly no bugs in practice and significantly increases the visual noise of your code.

If you have luxury of designing a new language or using one with default mutability then do so, but don't turn C coding styles into C++-envy, or C++ coding styles into Rust-envy.

3. maleldil ◴[] No.45773574[source]
> But it's a little too verbose to just sprinkle on every variable in C++

It's worth it, though. Every variable that isn't mutated should be const. Every parameter not mutated should be const, and so should every method that doesn't mutate any fields. The mutable keyword should be banned.

replies(1): >>45777172 #
4. loeg ◴[] No.45777172[source]
Const annotations are worth it for methods and APIs, but not most local variables.
replies(1): >>45778787 #
5. maleldil ◴[] No.45778787{3}[source]
I'd argue that it is. Use const by default, so it's obvious to the reader if something is mutated or not. It's easier to read code when you don't have to worry about how values may change over time. This is why Rust made the right choice.
replies(1): >>45783622 #
6. loeg ◴[] No.45783622{4}[source]
Yeah, I think it's a great default, and Rust got that right. It's just too noisy in C++ as the non-default.