TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Constantly Confusing ‘const’

33 pointsby _getifyover 9 years ago

9 comments

pornelover 9 years ago
I don&#x27;t agree with this style.<p>`var` is broken. Don&#x27;t try to find a use-case for it. Just don&#x27;t use it.<p>It would be nicer to have real, deep immutability, but `const` isn&#x27;t it. Therefore, use it for what it actually is, not what you wish it was. If you don&#x27;t intend to reassign a reference, then mark the reference constant. That&#x27;s it.<p>It may be surprising, but it&#x27;s quite easy to write JS where almost every declaration is `const`. When every mundane variable is `const`, then the more complex initializations&#x2F;data flows&#x2F;accumulators really requiring `let` stand out, which is very useful for reviewing code (&quot;let&quot; = &quot;warning: there may be an `else` or missing `switch` case that will leave that undefined&quot;).<p>Because `const` requires to be initialized at the time of definition, you know the variable is valid for its entire lifetime. When you find its value was wrong, there&#x27;s only one place where the value could have come from (you don&#x27;t need to comb the function to look for reassignments).
评论 #10187942 未加载
评论 #10188406 未加载
codecurveover 9 years ago
Why not use `const` as default? I would argue that using `let` instead is a premature optimization of readability.<p>It&#x27;s easier to reason about a variable if you know it will always be one value—even if the value may be mutated (which is unfortunate).<p>`const` is a simpler construct. It has less rules and I would always suggest that using `let` will introduce complexity that only needs to be there when you can identify a genuine need to re-assign a value to your variable.<p>That said, I enjoyed the post. It&#x27;s a good read!
评论 #10187538 未加载
评论 #10187337 未加载
aphexairlinesover 9 years ago
const conveys additional information. It says &quot;this name will not be rebound,&quot; not just to the reader, but to the machine interpreting this as well.<p>As a reader, I know that after reading a const line I don&#x27;t have to check if someone rebinds this name before each use site -- it&#x27;s impossible.<p>Interpreters and static analysis tools use this information to stop you when you write a piece of code that rebinds the name, asking &quot;did you really mean to do that? You said you wouldn&#x27;t.&quot;
评论 #10188166 未加载
Kenjiover 9 years ago
To be honest, I&#x27;ve been following the rule of using capital letters for things that should remain &#x27;constant&#x27;. If everyone working on the project knows that, it works like a charm, never had any issues with it. But it&#x27;s nice to have const now. So, I do see value in the const feature even if it doesn&#x27;t protect me against a commit by the devil himself who changes the content of const objects.<p>Long story short: If you want to be annoyed about this feature, then more power to you. I won&#x27;t be.
cbuqover 9 years ago
&gt; I think the likelihood is that a developer who needs to change a variable and gets a const-thrown error about it will probably just change the const to let and go on about their business.<p>If this is the core argument to not using &#x27;const&#x27;, then you are out of luck trying to enforce any coding style. I might as well just delete any code that doesn&#x27;t make sense.
评论 #10188853 未加载
PhiLhoover 9 years ago
One advantage const can bring (at least when used with primitives or frozen values) can come from JS IDEs: if they detect a value is const, they can report its value when hovering the variable name, in a static way.<p>That&#x27;s what Java IDEs do with static final variables.
mbrameldover 9 years ago
&gt; To most developers, it means that your LUCKY_NUMBERS will always be 12, 19, and 42.<p>Is that really true? I would assume the opposite. Most developers have used Java or C# or any of the other languages that have similar reference semantics. I would agree if you qualified that with &quot;beginner&quot;.
评论 #10188638 未加载
gadrfgaesgysdover 9 years ago
And people complain const in C is too complicated...
评论 #10188204 未加载
_getifyover 9 years ago
disclosure: self submission