Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
The reason to use const instead of let is because you want your code to be immutable. (just a note, const doesn't make object immutable, you have to freeze).
So how do you eat your groceries while being immutable?
Try to view immutable code as code with the added dimension of TIME!
// the pastconst{flour,eggs,turkey}=groceries// the presentconstcookedEggs=cookEggs(eggs)// the futureconstcookTurkey=turkey=>({...turkey,cooked:true})
At any moment in our application we can go back and look at what the state was before.
So imagine at 12:00 you had raw turkey. then at 12:30 you had cooked turkey. This is how immutable code works.
With mutable code, you know you have cooked turkey, but your past is invisible to you. You can never look at a previous state.
const is a good practice, but you even continue using let with immutable code. Sometimes itโs easier and more readable to use let for an immutable object, to which you are going to apply transforms.
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
@joelnet
I still don't need cookTurkey instead I can do turkey.cooked = true and so it doesn't matter whether I use const or let if my code is written keeping immutability in mind.
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
that is kind of why I mentioned '(just a note, const doesn't make object immutable, you have to freeze)' was to distinguish between const and immutable.
If you want a primitive value to be immutable, const is enough. If you want an object to be immutable, you have to freeze it as well.
So if we can assume groceries is immutable (which is should be).
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.
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++
Striving to become a master Go/Cloud developer; Father ๐จโ๐งโ๐ฆ; ๐ค/((Full Stack Web|Unity3D) + Developer)/g; Science supporter ๐ฉโ๐ฌ; https://coder.today
Not really, the "turkey" is a pointer to the turkey instance/value, so even if the turkey changes its state, the turkey pointer will remain constant, pointing to the same turkey.
Howโs it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK ๐ฌ๐ง
Education
11 plus years* active enterprise development experience and a Fine art degree ๐จ
Howโs it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK ๐ฌ๐ง
Education
11 plus years* active enterprise development experience and a Fine art degree ๐จ
This is an interesting point. What if some of the groceries where constant, such as the old tins you'll have in your cupboard for years. Seriously though, you can only have all constant or all let.
Howโs it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK ๐ฌ๐ง
Education
11 plus years* active enterprise development experience and a Fine art degree ๐จ
Howโs it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK ๐ฌ๐ง
Education
11 plus years* active enterprise development experience and a Fine art degree ๐จ
The reason to use
constinstead ofletis because you want your code to be immutable. (just a note,constdoesn't make object immutable, you have tofreeze).So how do you eat your groceries while being immutable?
Try to view immutable code as code with the added dimension of TIME!
At any moment in our application we can go back and look at what the state was before.
So imagine at 12:00 you had raw turkey. then at 12:30 you had cooked turkey. This is how immutable code works.
With mutable code, you know you have cooked turkey, but your past is invisible to you. You can never look at a previous state.
Thanks joelnet for following up with technical aspect of the code and the implication of using
const.const is a good practice, but you even continue using let with immutable code. Sometimes itโs easier and more readable to use let for an immutable object, to which you are going to apply transforms.
sure.
i just try my best to make everything immutable and handle mutable state separately, like in a redux store.
@joelnet I still don't need
cookTurkeyinstead I can doturkey.cooked = trueand so it doesn't matter whether I useconstorletif my code is written keeping immutability in mind.that is kind of why I mentioned '(just a note, const doesn't make object immutable, you have to freeze)' was to distinguish between const and immutable.
If you want a primitive value to be immutable,
constis enough. If you want an object to be immutable, you have to freeze it as well.So if we can assume
groceriesis immutable (which is should be).Then your groceries will truly be immutable
const. I hateconstin Javascript, because it's notconstat all - butfinal. In Kotlin, this'd bevalinstead ofvar- 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 turkeymeans an invariant Turkey.const Turkey * turkeymeans a pointer to a constant Turkey. In either case, you couldn't callTurkey::cook(), but you could callTurkey::price() const, since that's a const method. But in the latter case, you could assign (or "rebind")turkeyto point at a different instance. To prevent that, we can sayconst Turkey * const turkey, which is getting to be a mouthful. C++ also has references, which are non-rebindable pointers with easier syntax:const Turkey & turkeyis equivalent to that last double-const pointer example.All these headaches might be a good reason to learn Functional Programming... ๐ค
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++
Thanks Nick.
After reading your comment, I dug around and found this StackOverflow answer, which also explained using an example.
I wasn't aware that
nested objectcould be modified even after being frozen.Not really, the "turkey" is a pointer to the turkey instance/value, so even if the turkey changes its state, the turkey pointer will remain constant, pointing to the same turkey.
I had this imagination that, the ๐ฆ looks the same on the outside but cooked on the inside ๐.
haha, it sounds reasonable :D
Still not edible, take it back for a re-rest ๐
This is an interesting point. What if some of the groceries where constant, such as the old tins you'll have in your cupboard for years. Seriously though, you can only have all constant or all let.
Thanks Adam, you are right, I've realized that the syntax is for
all || nothingYou forgot a semicolon at the end.
I've been waiting for someone to point that out as well ๐คฃ
๐คฃ
Oh another thing, your destructuring references could be alphabetical.
Came for the fun, stayed for the conversation. Love all the comments!
Yupz, it's awesome how you learn from different perspectives and finding out faults so we could all learn ๐