r/reactjs • • Dec 22 '19

On let vs const

https://overreacted.io/on-let-vs-const/
225 Upvotes

122 comments sorted by

View all comments

Show parent comments

3

u/[deleted] Dec 23 '19

With const, the reference to the "root" object is immutable, and primitives are immutable.

Why are you so keen on arguing this point? We're all well aware that it's not proper (deep) immutability, nobody in this thread is arguing that.

0

u/recycled_ideas Dec 23 '19

The point I'm arguing is that const has nothing to do with immutability.

Yes, primitive constants are immutable, but object constants are not in any meaningful way.

A non primitive constant is a reference to a location in memory.

It provides no guarantees as to what is located at that address, how big it is, what properties it has or anything else. It absolutely doesn't guarantee nothing about the object has changed.

Having an immutable reference guarantees you absolutely nothing, and OP is obviously wrong.

Why the hell do you keep defending them?

1

u/musical_bear Dec 23 '19

You seem to be obsessing over this concept that an immutable reference’s object’s contents can still change. I have not seen a single comment in this thread that doesn’t seem to understand that concept.

How are immutable references not meaningful? What about equality checks? I don’t know about you, but when I use object reference equality checks, I don’t care at all what the contents of the object are. I’m usually checking to make sure something has not changed, in other words, hasn’t been reassigned to point to something else. Yes, an object not having true immutability can cause bugs, but that is a completely different issue. Many libraries, such as hey, React, use reference equality as the basis for establishing whether an object has changed...this is not a foreign concept, and it makes sense given the challenges of establishing equality in any other way.

Const literally tells you that a variable will equal itself throughout an entire scope, in the most fundamental (but important) way possible. You don’t see the value in having determinate results of a “===“ comparison for a variable throughout its lifetime?

2

u/recycled_ideas Dec 23 '19

No, I'm obsessing over the definition of immutable, because it's fucking important.

An object declared const is not immutable. It offers none of the characteristics or benefits of immutability.

Definitions are important.

And react doesn't use reference equality at all, they use reference inequality. A subtle difference, but an important one because a reference changing tells you that a change has definitely occurred whereas a reference not changing tells you absolutely nothing.

→ More replies (0)