Domanda

Referring primarily to here, it suggests that values which are constant in JavaScript (using the keyword const) should be named in SHOUT_CASE. I'm of the opinion though that mutability is much more important (and rare) than immutability, at least in JavaScript, and that having so many variables put in SHOUT_CASE would actually harm readability, rather than aid it, and dilute the meaningfulness of the convention itself.

Now, I understand that SHOUT_CASE for constants is useful in languages that do not have inherent support for constant values built into the runtime - for example, ES5 javascript, where you had var and nothing else. But with language-level support for const values, is there much use for this convention any more?

At runtime, any identifier created using the keyword const cannot be re-used or re-assigned to. This isn't strictly const correctness in the C/C++ sense, but for primitives it is fine. For objects, you'd have to use Object.freeze to get const-correctness. JavaScript is far from the only language to do this, of course. Fields are commonly readonly (C#) or final (Java) [citation needed].

What benefits would having things labelled in SHOUT_CASE present in a language that already has const support built into the syntax?

È stato utile?

Soluzione

The main benefit of using SHOUT_CASE for constants is to easily know when reading code when a particular value is not expected to change without having to look for the definition, or worse, having to search for every reference to that variable to see if its value changes.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
scroll top