DEV Community

Discussion on: What's wrong with this code?

Collapse
 
dwd profile image
Dave Cridland

const. I hate const in Javascript, because it's not const at all - but final. In Kotlin, this'd be val instead of var - but even then, this is a fixed binding, and not an invariant.

So you can cook the turkey, for sure, you just can't change which turkey you're cooking.

In C++, on the other hand, const Turkey turkey means an invariant Turkey. const Turkey * turkey means a pointer to a constant Turkey. In either case, you couldn't call Turkey::cook(), but you could call Turkey::price() const, since that's a const method. But in the latter case, you could assign (or "rebind") turkey to point at a different instance. To prevent that, we can say const Turkey * const turkey, which is getting to be a mouthful. C++ also has references, which are non-rebindable pointers with easier syntax: const Turkey & turkey is equivalent to that last double-const pointer example.

Collapse
 
dance2die profile image
Sung M. Kim

All these headaches might be a good reason to learn Functional Programming... 🤔

Collapse
 
misterhtmlcss profile image
Roger K.

And now I'm reminded why I prefer to code in Python or JS. Listening to the C++ version hurts my brain. A fragile Front-End guy and I'm thankful to not be doing C++