Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
kamen69172yThe const keyword has one very specific purpose - namely preventing accidental reassignment - and it's doing great at it.
-
@kamen I’m curious. How does it do it exactly?
I mean since it’s not compiled, it can only be checked at runtime and then throw an error or something.
If it works like that, then I agree with OP: It’s useless. -
Ok, maybe not entirely useless. But runtime enforced immutability is so far away from the usefulness of true immutability that it feels like an insult to call it "const".
-
Marl3x26782yif you ever feel useless: remember that this rant exists, which you read, scrolled into the comments of, and got pissed. Value your time.
-
Who cares if it happens at run time, it's like saying Typescript is useless because it happens at build time. Sure from a software perspective it's a little curious, but from a developer experience perspective it is absolutely fantastic.
It's all about making programming safer, not the program. -
hjk10156962yYes scripting languages so everything at run time... However the language constructs do not have to be checked only at run time. Your IDE can report an eye when you are modifying a constant.
-
I first use const, then let and rarely var. They all have a purpose, well, if you don't write shitty code.
-
@melezorus34 yes, const prevents reassignment. Freeze prevents modification of the object.
My criticism about the claimed usefulness of constants was not about that.
It was about the fact that nothing prevents you from ignoring the declaration of something being a constant at development time.
You will find out that you did a mistake when it manifests as a bug in production on the end user’s machine.
This is the safety aspect of the usefulness of constants.
There is also the performance aspect:
When it’s enforced at runtime, it costs performance. Compile time enforcement enables optimizations and can improve performance. -
@Lensflare i take your point with a grain of salt and add:
If you have an IDE like geany and got so far to push PRODUCTION code with it, you got mad luck.
Every fucking [javascript supporting] IDE ever shouts out "bro you just tried to re-assign on a const, that's cringe, you can't re-declare with let bro" and those are shouted as errors because they fucking are.
Even if you don't have typescript, eslint, a CODE WRITING STYLE enforcerer will complain about it too. -
Adding to the point "when it's checked at runtime, it's useless"*:
People write python code and push to production. (lmao no consts)
People write lua code and upload to Steam Workshop.
People write CSS and upload it to a site.
In every one of them, it will always be a runtime error. (But css don't has const? any error thrown in css is in runtime, that was my point) -
Voxera113972y@Lensflare most dev tools will warn even if js is not compiled unless your using plain text editor with no tooling.
-
Voxera113972y@Lensflare just because js is not compiled does not mean it cannot be checked using lint tools that have been around for decades.
Thats not a language problem but dev environment problem.
Like mentioned above, all interpreted languages can have the same problem unless you use some extra tool to test things.
Adding const helps those tools a lot to know the intent.
Without it the tools will not know the intent and cannot warn. -
@Voxera yes, I see. Still, the usefulness of const then becomes more of a property of the IDE and not of the language itself. The original post was about "const in JavaScript".
But I agree that in practice it‘s still more useful than I thought. -
Voxera113972y@Lensflare well, they cannot change js to be compiled so adding const was the next best thing.
Related Rants
If you ever feel useless, remember that `const` exists in JavaScript
random
js