r/javascript • u/kasperpeulen • Oct 24 '16
help The only thing I hate about const ...
... is that it is 5 chars long.
For some reason, let with its 3 chars, looks more elegant.
8
7
u/Retsam19 Oct 24 '16
I use let so infrequently that it's pretty rare that I actually see const and let mixed.
7
Oct 24 '16
Somehow I find myself agreeing with you...
4
u/ebilgenius Oct 25 '16
let agree = true;22
u/snoee Oct 25 '16
agree = false;
Sucker, shoulda used const!
5
u/inu-no-policemen Oct 25 '16
For opinions? Certainly not. You should always reevaluate your stance whenever new information becomes available.
6
Oct 25 '16
Still possible if your brain uses uses redux:
const agreeReducer = (opinion = { agree: true }, thought) => { const nextOpinion = Object.assign({}, opinion); if (thought.action === 'CHANGE_MIND') { nextOpinion.agree = !nextOpinion.agree; } return nextOpinion; }With the added benefit of being able to change opinions in a well-informed manner because you are able to keep perfect track of prior reasoning.
2
4
u/saadq_ Oct 24 '16 edited Oct 24 '16
Man, this sub is volatile, haha. I got a pretty negative reaction when I mentioned the same thing a few months ago.
I agree with you, especially in cases where you have a mix of const/let variable declarations like:
const var1 = []
const var2 = {}
let var3 = 30
let var4 = 40It would look neater if you used let everywhere (although of course you lose the added safety of const)
let var1 = []
let var2 = {}
let var3 = 30
let var4 = 40Although I have seen a lot of people who do use let everywhere, and only use const for imports and for the old way of declaring "constants":
const fs = require('fs')
const MINIMUM_PAYMENT = 5
// Use `let` for everything elseI know Mozilla does it that way ^
4
u/Retsam19 Oct 24 '16
In fairness, "const is 5 letters long" is pretty off-topic in a thread about the performance implications of
const; I'm not surprised you got flak for that reply.1
u/saadq_ Oct 25 '16
Yeah that's true, but based on the responses it seemed like it had more to do with complaining about
constrather than it being off-topic.1
u/factorysettings Oct 24 '16 edited Oct 25 '16
const var1 = [], var2 = {}; let var3 = 30, var4 = 40;?
1
u/saadq_ Oct 25 '16
I guess the argument I was making is that it looks nicer if you use exclusively one out of
const/letbecause everything can line up:let var1 = [], var2 = {}, var3 = 30, var4 = 40;1
u/inu-no-policemen Oct 24 '16
I know Mozilla does it that way
I also do it that way. If I have a handful of short-lived variables, I don't really care if they get assigned some value just once or more than once. The write and read aren't that far apart and you have to read both lines either way. There doesn't seem to be any tangible benefit. It doesn't simplify my mental model.
I'd only go with immutable bindings everywhere by default if they were less work. E.g. like "let" vs "let mut" in Rust.
What actually would simplify things is if the editor would color single-write variables (no matter if it's const or a single-write let) differently.
2
1
u/ghostfacedcoder Oct 24 '16
Amen. What was wrong with "con"?
10
1
1
u/PitaJ Oct 24 '16
It's not a reserved word.
3
u/inu-no-policemen Oct 24 '16
That's true.
constwas a reserved word long before it got actually used. It already was in ES1's list of future reserved words. It has been there since 1997.import,export, andclassalso have been there since the beginning.1
1
u/Gravyness Oct 25 '16
Didn't know people worried about that too. One of the things I liked about var is that you can type it with just your left hand.
Although it is useful and intuitive, I dislike the use of const, I'd rather just have it in the start of my code, usually for require()s.
1
-2
u/kevinkace Oct 24 '16
Agreed, for primarily aligning single const declarations, eg:
let thing1 = "thing1",
thing2 = "thing2"; // nicely aligned
const thing3 = "thing3",
thing4 = "thing4"; // like this with standard 4 spaces?
const thing5 = "thing5",
thing6 = "thing6"; // or like this to align?15
u/rauschma Oct 24 '16 edited Oct 24 '16
I prefer:
const thing7 = "thing7"; const thing8 = "thing8";Easier to rearrange (especially if git is involved) and automatically aligned.
1
u/saadq_ Oct 24 '16
But what if you have a mix of
constandlet?const thing7 = "thing7"; let thing8 = "thing8";I imagine you wouldn't do this:
const thing7 = "thing7"; let thing8 = "thing8";1
u/rauschma Oct 24 '16
Yes, that does make things more complicated. I’d probably put the
lets last, without any additional spaces before the variable names (as in your first code fragment). Looks a bit ugly, but does clearly separate groups ofconsts from groups oflets.8
u/konbit Oct 24 '16
If you work in a team using git this is a horrible idea anyway. I can't count the amount of times code has broken after a merge because of this stupid pattern.
-8
u/kevinkace Oct 24 '16
Then you suck at merging.
4
u/adavidmiller Oct 24 '16
That's kind of the point. You shouldn't depend on people not sucking if you don't have to.
2
-2
u/56k_ Oct 24 '16
You need to learn how to type. 5 characters (see what I did here..?) is not a lot.
2
u/kavisiegel Oct 25 '16
haha! there's a joke buried in here! there really is! javascript - learn how to type! haha! hilarious!
0
u/Bummykins Oct 25 '16
Totally! Also, its a much bigger chance to spell it wrong. Today, after I misspelled it 3 times in a row, I thought very hard about making a shortcut for c+tab -> const. Maybe tomorrow.
2
0
u/Graftak9000 Oct 25 '16
Ha, that's why I don't like it, it interferes with my
consoleauto-completion.
0
u/0x13mode Oct 25 '16
const has other disadvantage, as well. You can't postpone assignment, e.g.
const a; //error!!!!! Uncaught SyntaxError: Missing initializer in const declaration
if (true) {
a = 1;
} else {
a =2
}
but you have to use let. Or var.
2
u/AceBacker Oct 25 '16
This is a good point. But to counter. Usually these postponed assignments work great as a short function to return the value.
const a = getTheValueForA(someCondition);1
u/0x13mode Oct 29 '16
you're right (you and @atubofsoup) that sometimes function/IIFE can be solution to that (especially if we have simple
=>syntax)1
u/CWagner Oct 25 '16
const t = a ? 1 : 2;
1
u/0x13mode Oct 25 '16
this is simplest case and sometimes
? :is god but sometimes/often? :it's not very readable (complex conditions etc.)Besides this doesn't have to be condition. Problem is not with a condition but that
constforces to assign variable during declaration. Even if you're planning to assign variable just once and treat it as a constant after that, you can't do it using const (afaik).3
u/atubofsoup Oct 25 '16
You could use an IIFE:
const foo = (() => { const a = 5; const b = 10; return a + b; })();It's not very pretty though.
1
u/0x13mode Oct 29 '16
you're right (you and @AceBacker) that sometimes function/IIFE can be solution to that (especially if we have simple => syntax)
10
u/Pyrolistical Oct 24 '16
Could have gone val