←back to thread

207 points mfiguiere | 2 comments | | HN request time: 0.57s | source
Show context
stickfigure ◴[] No.43539275[source]
Great! Now can we make `final` the default for all fields, variables, and parameters?

(yes yes, I know, that would break syntax... but please come up with something to discourage mutability)

replies(5): >>43539333 #>>43539950 #>>43540550 #>>43541493 #>>43543176 #
magicalhippo ◴[] No.43539333[source]
Const-ness in C++ is something I miss in other languages. Being immediate able to see that this function or method couldn't mutate the object made it so much easier to reason about the code.

Yeah I know there's ways around it, but then the author known what they told the other party to expect.

replies(3): >>43539580 #>>43540000 #>>43555589 #
1. titzer ◴[] No.43540000[source]
If possible, 1) design completely immutable data structures that can be broadly shared and don't need to be copied. If you need mutability, just embrace the fact that someone is going to abuse mutability and 2) try to create abstractions that can suffer abuse. If you're coming from a language that doesn't have const, you learn to build things that are hard(er) to screw up.

While bad code can exist in any language, I get worried about too much const in code, because it means they failed at both 1) and 2) and instead there are usually seriously tricky protocols that must be observed to make the thing work. I often ran into code where people were sprinkling const all over the code to lock things down but they fundamentally did not understand the design and made it nearly impossible to evolve, unless you used casts to get rid of const, which defeats the whole purpose.

I'm not saying const doesn't have value, but it's weapon #3, not weapon #1.

replies(1): >>43540677 #
2. magicalhippo ◴[] No.43540677[source]
> sprinkling const all over the code to lock things down

That's like using a hammer on a screw, clearly not the right way.

Thankfully I've never worked on such codebases.