Let me be
Ryan Florence delves into the evolution of JavaScript, with a particular focus on the ES6 feature const
. The discussion dives into the let
and const
keywords, elements that have had a transformative impact on JavaScript practices, but also stirred noteworthy debates within the coding community.
Misunderstandings around the const
keyword were addressed, with emphasis on its role as a constant and not a variable. Moreover, Ryan clarified that using const
does not inherently prevent content mutation. The complexities involved in achieving immutability in JavaScript were also discussed.
The talk underscores the influence of entities such as Udacity and Airbnb on JavaScript coding conventions, stirring a dialogue about the enforcement of coding standards that are not always consistently applied.
In examining the implications of ES6 features, Ryan suggested that strict adherence to rules around variable declaration may not significantly enhance code quality.
In an unexpected twist, the session also ventured into the phonetics of language, specifically the pronunciation of JavaScript keywords and how hard the word const
is to say.
The talk concluded with a challenge to the audience to broaden their understanding of JavaScript's language features and their applications, highlighting the urgency to focus on user experience over strict coding regulations.
Share this talk with your friends
Transcript
Got to install Docker real quick. Oh, got it. Okay. Hi.
As my five minutes started, I'm just getting trolled. I got three minutes, 33 seconds. What? Three minutes? This is going the wrong way. Anyway, I'm starting right now. All right.
My name's Ryan, and I'm not going to talk about Remix. We're going to talk about Dynamite real quick. Dynamite was created for mining. But what do people use it for? To kill other people in wars. Much to the chagrin of this man, Nobel, that's his last name. You've probably heard of
the Nobel Prize, specifically the Nobel Peace Prize. So he was probably pretty upset that his invention was not used for the thing he wanted it to be used for. Anyone good at chemistry know
what this is? That's LSD. For some people, it might be water. This is the guy who invented it. There's no prize named after him, so I can't remember his name. It's like Albert or something
like that. Hoffman. Okay, we got LSD fans here. I don't even drink coffee, so this is as far away from anything that I'm putting in my body. So he was trying to create a compound to treat ADHD,
a central nervous system stimulant, or something like that is what it was. He was trying to treat ADHD and hyperventilation from panic attacks. He was trying to help people. I don't know, I guess maybe some people microdose LSD and they say it helps them. But it's pretty undisputed that
LSD has wrecked many, many lives. So I don't mean to say that too jokingly, but this is kind of a fun talk. He invented LSD and it got used for things he didn't intend. Reading his Wikipedia
page, he continued to take LSD his whole life and kept trying to find a different use for it
and never did. This is the guy who invented Q-tips. He's like, it's for cleaning your ear, yes, but not
that part. It just makes it worse when you just keep like shoving the thing into your ear, right? And they got it all over the morning, but has anyone used a Q-tip for anything besides trying to get earwax out? No, it's the only thing we do with it. Here's another guy who created something.
Dave Herman. Some of you who are old hats like me remember him. Ten years ago, ES6 was all the rage. It was like JavaScript puberty. It was like coming of age and it could actually do cool things now and we're taking it very serious. React hit the scene. We're building really interesting
websites and so the language needed to get a little bit better. Dave Herman was someone who was really instrumental in pushing the language forward and he's a hero of mine. He's one of my web heroes. Some other people obviously were involved too, but Dave was talking a lot about
ES6 and some of its features. And here is one of his talks, a little screenshot. Notice that it says let is the new bar. What's missing from his slide? Const is missing. You can go and search all of his ES6 talks and slides and comments. Doesn't talk about const.
So I wonder what he has to say about it. The guy who gave us let and const. I wonder if he's like the guy who gave us dynamite and LSD and Q-tips. Or maybe he loved it. So Pamela Fox, another web
hero of mine. I'm this close to holding a company meeting about when we should use let versus const. Anyone had that meeting? No, we don't even do that anymore, right? We just use const. Dave Herman, I'm sorry. Why cats? Yehuda cats. Warned me const was a mistake for this exact
reason and I didn't take it seriously enough. One of my main ES6 regrets. The guy who gave you const regrets giving you const. Does he just feel bad or did this ruin him? PSA, const is short for constant, not functional programming.
Y'all think you're a bunch of immutability. I use const. I'm going to learn OCaml one day. JS const should never be used for anything but constants. Using const to mean the variable happens to not be reassigned makes your code less readable, not more. You can clap, John. Let's just do one clap though because that's how it feels being a fan of let. It's like it's you
and everybody else. I'm going to do a second edition of Effective JavaScript just to shoot posts about const and print and incite flame wars in the Amazon reviews. I might be a jerk but I don't swear. I got the important things down.
Sorry for the const tweets, folks. I'm feeling much better now. We actually ruined this, man. This tweet has nothing to do with my talk but look at the timestamp. This is his last tweet. He hasn't tweeted in a full gestation period. And his tweets before that were sparse too. All those tweets I showed you before,
this is all like 2017, 15, 14. And then he's kind of tapered off. We wrecked him. So how did this happen? How did we get here? Why is everyone doing const everywhere? Or sorry, rather. So what is it for? Yehuda Katz also was instrumental in ES6 and he says,
the TLDR for that meeting that Pamela was asking about is const for top level module scope constants only and screaming case only. Otherwise, let and call it a day. So the people who put this into the language think you're all crazy.
So how did we get here? That wasn't intentional but that is how I feel when I see an Airbnb ES link config in a project. I was at Airbnb giving them a workshop with our company React Training
and this came up, the Airbnb ES link config. And they all started laughing like, oh, we don't use that here. So the people enforcing this thing don't even use it either. But you're still doing
it, right? It's very important that I know when my variables are reassigned. Okay, that's fine. If you want to use const instead of let all over the place, I'm not going to tell you what to do, but there are a couple of things that I want you to understand about what you're doing. So if you ask the question, how should we do variable assignment?
That is an invalid question because consts are constant. They're not variables. They don't change. So you can't say to your co-worker like, oh, what's the value in this variable? You need to say what's the value in this constant. You're going to find this very difficult because you're calling all of your const variables and they're not variables.
They're constants. Mutation is difficult to manage in the large and deserves careful annotation. Const doesn't help with this. Arrays, you can push into it. Objects, you can assign to them. Const didn't help you with mutation here, which is why we use const, right?
So if you're going to use const, I insist that you also use object freeze because you're saying I want to indicate intent here. I'm not going to mutate this. So you got to freeze the object. We're a bunch of React developers probably in here, either happily or unhappily or something
in between, like me. So you're going to actually do it like this, right? Oh, I better memoize it because I bet object freeze is expensive. And then it's like, oh, but I got that dependency array, so I got to freeze that one. Oh, and brands is an object, so I got to freeze that too.
So if you actually want to protect yourself from mutation, this is what your code's going to look like. This is the logical conclusion of what you're doing. Bad news, when you pass that object to a function, those ratings, this one that we were just looking at, it's memoized. It's got all the tricks in it, right? We've frozen everything in it. It's
memoized. You pass it to a function. That's reassignable. Oh, what am I touching? Oh, it's this. Oh, they turned it off. I was going to do a little Tom Morello, but you can reassign that. You cannot make a parameter to a function a constant.
So there comes a point where you can't keep doing what you're trying to do to tell yourself that you're protecting your code from something. So you can't freeze your parameters. So I asked ChatGPT, has using const everywhere in JavaScript instead of let ever prevented
a bug in any real code in production? Said no. Though using const instead of let in JavaScript is a common practice, it has never prevented a bug in any software that I've been trained on. It's typically recommended by people who need easy-to-follow rules to feel like their code is
good. Unfortunately, it is a confidence-boosting distraction from the real work of building a great user experience. That is all of human knowledge came up with this answer for us, okay?
And if you're still not convinced, is this mic working? We're going to talk about acoustic phonetics. We've got plosives, like explosive, like T, T, T, C, K, K. So your tongue hits somewhere and then a bunch of air comes out, right? We call these stops also. We've got fricatives,
so that's like you make a small hole and push air through it, S, S, and F. And we've got nasals, like N and M, where we move air through our nose. We've got liquids for L and R. We make a shape with our tongue and then we let air flow around to L. And then we've got vowels,
where you just kind of open up and let your vocal cords vibrate, right? So let's walk through let. Let. Let. What do we got? We've got a liquid at first, and then we've got a vowel. And you think we have a stop with the T, right? But we don't say let. We say let, like a Utah person says mountain.
It's just let. There's no T then. So all we have is a liquid and then a vowel, and then we just kind of cut off the vowel. Let. You can say it a whole lot and not run out of breath. Let, let, let, let, let, let, let, let, let, let, let, let, let, let, let, let, let, right? And I'm not going
out of breath, I can keep talking. I'm up, huh? I'm here. Const starts with a plosive. Back of your
mouth, back of your tongue hits the top of your throat, right? And then where does the air go? Ah. So now we open up and have vowel and let this air flow out. And then we go, so then we move the air back up into our nose and our tongue goes up to the roof of our mouth. So now the air is coming out of our nose. Then we go, so now we've got to move it back to our
mouth. Our tongue comes up and then the air comes out through our teeth. And then we've got to do this plosive at the end. And so our tongue comes back up and then the air flows out. It's ridiculous. So please just let me be.