YTread Logo
YTread Logo

`const` was a mistake

Jun 29, 2024
A couple of weeks ago I filmed a video All about con versus let because well, it's an interesting topic to me and I have a stance which is that both are fine. I tend to use con when I can. There is a phenomenal blog post by Jamie builds builds ghost Jamie. Kyle, whatever you want to call it for a couple of years now, it's actually quite a bit longer than what I've used as a reference for a while and that's what I used in the video, it's not that

const

means something, it's that let means something . and when you're using let, it should be with some kind of intent and it should communicate something to your team right before you publish that video that I went to Epic webc.
const was a mistake
I didn't expect Ryan Florence to be there, much less to give. a much less talk so that talk was cons versus leaving which immediately sparked the debate once again. I could have posted the video I already recorded but so much has happened since then and we've had so much discourse and even people I didn't expect. As I read Rob moving towards the allowed side of the camp, I felt like we needed to reshoot and make a new video. All about cons versus leaving, but if you're interested in that other video, I'll have it all on Theo.
const was a mistake

More Interesting Facts About,

const was a mistake...

Disposable channel, if you don't know, it's all the videos that I really don't know how to make a thumbnail or a title for or I don't think it fits here, check it out anyway, let's get into this wonderful debate of cons versus leave and maybe I'll even mention something, since I said this talk by Ryan Florence was what started the debate again, so it's only fair if I dive into it. Hi, that's an interesting opening slide. Oh wow, since my five minutes have started, I'm getting trolled. My name is Ryan and I'm not going to talk about remixes.
const was a mistake
Let's talk about Dynamite real quick. Dynamite was created for mining, but why were people using it to kill other people in Wars much to the displeasure of this Nobel man? That's his last name, very interesting analogy. I'm sure this won't be used for any kind of scope. You've probably heard of the Nobel Prize, specifically the Nobel Peace Prize, so he was probably pretty upset that his invention wasn't used. because I wanted anyone good at chemistry to use it, you know, this is water, that's LSD, for some people it might be water, this is the guy, take enough LSD and I'm sure it looks like water, the guy who invented it .
const was a mistake
No, like the award is named after him, so I don't remember his name, it's like Albert or something, Hoffman, okay, we have LSD fans here. I love the talk that he immediately knew who he was. I don't even drink coffee, so that's how it is. so far from anything I'm putting in my body, so he was trying to create a compound to treat ADHD, a central nervous system stimulant or something, is what it was. I was trying to treat ADHD and hyperventilation like panic attacks, I was trying to help people, right, and uh, I don't know, I guess maybe some people take LSD and say it helps them, but it's pretty inarguable. that LSD has ruined many.
This is supposed to be a five minute thing and we. It's been almost a minute and so far he's talked about dynamite and LSD in many lives, so no, no, no, I don't mean to say it as a joke, but this is a fun talk and, uh, he invented LSD. and he got used to things that he didn't intend to uh reading the Wikipedia page of him he continued taking LSD his whole life um and uh he kept trying to find a different use for it and he never did this is the guy who invented Q-tips. one of the few people who uses Q-tips for many things like cleaning computers and they are very helpful but fair.
I'm excited to see how this ends in cons versus Le right now I'm feeling excited. audience, this isn't even my talk, he says it's to clean your ear, yeah, but not that part, it just makes it worse when you keep shoving the thing in your ear, so, and everyone's done with the warning, but like . Someone used a Q-tip for anything, that's why we disagree with Ryan, because I'm always the person who actually uses the object the intended way, but yes, I use Q-tips in many intended ways for cleaning. all sorts of really nice things besides trying to get ear wax out, no, that's the only thing we do with it.
Here's another guy who created something. Dave Herman. um, some of you who were old like me, remember it. 10 years ago, es6 was all the rage. JavaScript was like the puberty of JavaScript, it was like coming of age and it could actually be the good HTML 5 es6, generating good times like doing cool things now and we are taking a very serious reaction to come on the scene and we are building websites really interesting. and um, so the language needed to improve a little bit and um Dave Herman was someone who was instrumental in pushing the language forward and he's a hero to me, he's one of my web heroes um, some other people were obviously involved as well um , but Dave was talking a lot about uh es6 and some of its features and here is one of his talks, a little screenshot that says let is the new VAR, yes it says that because VAR is not replaced by

const

VAR in its cases of intended use. replaced with let because it's a variable that should change let is letting it change oh boy what's missing on his slide is missing const you can go and search all his talks and slides and comments es6 doesn't talk about const so me I wonder what the guy who gave us let and const has to say about it.
I wonder if he's like the guy who gave us dynamite, LSD and Q-tips or maybe he loved it, so Pamela Fox, another of our heroines, I'm that close. having a company meeting about when we should use let versus const someone had that meeting in your is this kind of meeting God are you playing this video in that meeting oh no I'm so sorry anyway no we don't even do that anymore that? true, we just use const Dave Herman. Sorry, why cats? Yehuda cats. I actually didn't know this. Dave Herman regretted it. Take it, I didn't take it seriously enough one of my main es6 regrets the guy who gave you const regrets giving you const he just feels bad or this ruined him PSA const is short for non-functional constant programming this is personal attack I'm offended I see how you all think they are B immutability I use with I'm going to learn o camel someday I intend to learn oaml someday, but that has nothing to do with what we're talking about here, Ryan , I understand that the cons are not proper immutability and I understand that wanting to learn about camel makes me a tool, these are two separate and unrelated things.
JS con should never be used for anything other than constants using cons to mean the variable is not reassigned makes your code less readable, you can't anymore, this is another one of those times where the art is removed from the artist, right? is that so? We're not going to agree, are we, Dave. I'm so sad the list is growing. A longer round of applause, John, let's just give it a round of applause because that's what it feels like to be a Let It fan, it's like it's you and everyone else. I'm going to make a second edition of Effective JavaScript just to publish posts about constant printing and nighttime flame wars. on Amazon reviews, thank you for doing so.
PG ran. I may be an idiot, but I don't swear. I wrote down the important things. I didn't know Ryan didn't say bad words. Makes sense. Sorry for the scam tweets. I feel a lot. better now we actually like it we ruined this man this tweet has nothing to do with my talk but look at the timestamp when you are sleep deprived to keep trying to stay awake through the night you might think you can achieve things, but It turns out that in such situations the lack of resources gives you a lot of problems in achieving things.
It turns out that this is the last tweet of his that he hasn't tweeted in a full gestation period and his previous tweets were also scarce, like all those tweets I showed you. before this everything like 2017 15 14 and then it just slowed down like we smashed it so how did this happen? How do we get here? Why is everyone doing const everywhere or sorry rather? What is Yuda for cats? instrumental on es6 and it says that the T LR for that meeting pamelo was asking about is Con only for top level module scope constants and only case of shouting, otherwise leave it and call it a day so the people who They put this in the language think you're all crazy, these are the people who created JavaScript.
I think between the people who created JavaScript and the people who use JavaScript, we can all agree which one is crazier, right, we're talking about the same programming language right now. Right anyway, how did he do it? we got here, that was unintentional, but that's how I feel when I see an Airbnb link setup is in a project, yeah, nice Airbnb link setup, wait, no, oh no, I just realized towards where it goes. configuration existence is lint to the general industry preference towards using const because I don't like those things being linked, which makes me uncomfortable.
I was at Airbnb giving a workshop with our company on reaction training and this came up on Airbnb. it's link config and everyone started laughing like oh we don't use that here so the people who enforce this don't even use it yeah no one no one uses this that includes it including Airbnb which is funny this setup is What a mess, it's so horrible, please, if you're actually using this, for some reason, throw it away, just throw it away using all the built-in Eslin typescript stuff, it's so much better. The Eslin configuration recommended by typescript is really good, just use it. but you're still doing it right, it's very important that I know when my variables are reassigned, okay, okay if you want to use if you want if you want to use Le or cons instead of carry everywhere.
I'm not. I'm going to tell you what to do, but there are a couple of things I want you to understand about what you're doing. If you ask how we should do variable assignment, that is an invalid question because conss are constants. They're not variables, they don't change, so you can't say to your coworker, oh, what's the value of this variable? You have to say what is the value of this constant? This will be very difficult for you because you are calling everyone. Your conss might remind you in this wonderful language we're discussing here, there are a lot of things you can do with const and let.
If I have a function here, do some work, let do some work, equals async, this is totally valid as well. What is this variable because let means variable? No, in this case let means a function and this is also very dangerous because this function can now recover where I could set do a job equal to console.log do a job and I just broke everything, what's making me angry? Oh, because the type is a promise, so I have to make this asynchronous so that the types match. Oh, the input type isn't the same either, so form data, form data, can I get this respected? empty just empty no, you get the point, typescript is saving us from doing something really stupid here, it doesn't really want us to do it, which is good because it's really stupid the idea that we can have a function that exports or uses this way God We prohibit this from being exported from one file to another file and now we can override it, it is not good and just changing this to constant fixes that now we will get an error that it cannot be reassigned to do some work because it is a constant. using let const for functions depends, there are arguments for both, there are arguments against both, but hopefully, when you say we should use let not const because it's a constant, not a variable, you don't have a single arrow function in none of your code, because if you're saying let is for variables and con is for constants, then you recognize that you're just not acknowledging the existence of error functions at all, so yeah, that was a little Bullit variable and they're not variables .
Mutation of constants is difficult to handle in general and deserves careful annotation. The constant doesn't help with these arrays, you can insert objects that you can assign to them. The cons didn't help you with the mutation, yes, but you can't replace the element. You cannot make a matrix of new brands, which I think is a very important detail. If I have the same brands, sure and here I want to have different brands. I can't just change brands to like Nike, Adidas, Puma and here I want to have different brands. I can't just change brands to like Nike, Adidas and Puma because it's a constant and ideally if you wanted these here to make brands. popop like while ticking.
The length is greater than zero marks. pop and then marks. press for all this because this would be the alternative. map the flag like, could you force this remapping with something like this? Sure this is a much bigger code smell than Brands equals Nike Adidas pumar rebok again, sure you can make changes to Brands, but at least the changes seem more deliberate and require more work and are less likely to be someoneaccidentally reassign the thing, don't even catch it, which by the way, once you've exported this, if it's export, let Brands have good luck and have fun here, which is why we use const correctly, so if you're I'm going to use const.
I insist that you also use object freezing because you're saying that I want to indicate intent here. I'm not going to mutate this, so you have to freeze the object. We're probably a bunch of react developers here. or like happy or unhappy or something in between like me, wow, so you're going to do it like this, good! Oh, I better memorize that because I bet freezing objects is expensive and then it's like ah, but I have that dependency array. so I have to freeze it, and Brands is an object, so I have to freeze it too, so if you really want to protect yourself from mutation this is what your coat will look like this is The logical conclusion of what the goal was never was to protect us from mutation is give the semantic indication again like this is the topic I mentioned in the original video which is probably in the The second channel could be here, who knows where it ends.
The central point I've made is that it's not that const means something specific, it's that let means something specific when we use let, the intention is to replace the element, that's what we put let here. because this could be remapped at the root level somewhere, it's a very explicit and specific thing we're doing and we're doing it for a reason for those C and C++ engineers in the chat. I have a tweet that I made. Let's see if I can. I find that I think sums this up very well from a memory standpoint, very much like Ram.
JavaScript objects cannot be immutable. How wonderful, yes, he is a star. We are going to be a star. Star. It was the point I made here, that if you're not familiar with the star. is assigning a pointer, which means that instead of just having a value, it points to a part of memory that you can't change, you can change what's there in memory, but you can't change what point in memory it points to . a pointer to a pointer which means we can change where it points to so if we want to reassign the value to another value somewhere else we can do that, if we want to change which pointer it points to we can and that's the difference with cons versus let is that the cons point to a specific object, you can't change what it points to, but you might be able to change the content of what it points to if you want to change what object the variable points to, that's when let comes in handy and this is a significant option. difference like imagine if in C++ people said never use pointers, always use double pointers and I think we can all agree that that would be a little crazy and that's why I'm so excited about this.
I don't know if it's because I spent my days in C++ or whatever, what you're doing is bad news when you pass that object to a function, those qualifications, this one we were looking at is memorized, it has all the tricks, right, we've frozen Everything it contains has been memorized for you. pass it to a function which is reassign oh what am I touching? oh, is this, I'm going to oh, they turned it off. I was going to do a bit of Tom Mel, but you can remap it like you can't cast a parameter to a function in a Conant, 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 just can't freeze your parameters so I asked the GPT chat. has ever prevented a bug in any real code in production and said no, although you used const instead of letting JavaScript as a common practice, you have never prevented a bug in any software in which I have received training.
Recommended by people who need easy-to-follow rules to feel like their code is good, unfortunately, it's a confidence-boosting distraction from the real work of creating a great user experience. We have to use const everywhere in JavaScript instead of letting it ever prevent an error. in any real code in production it's like this, real code in production, yes it's apparently poorly written so this is almost certainly real in the same sense that the blog posts I read that tell you to subscribe are real. by the way, what is misspelled was written here, oh ER Cod is good, yes, it is most likely fake because there are typos here and the chapter is not known to do much the same way, sometimes my posts blog when I read them they say to subscribe, speaking of which, our response seems to think that you should subscribe on YouTube, so if you don't have it, the sub button is right below, it's free, feel free to subscribe, we make a lot of content like this.
I think it's a fun channel and even chat gbt seems to think you should subscribe like crazy I know but yeah you get the idea that all human knowledge gave us this answer okay and if you're still not convinced oh this one argument, the pronunciation is this. microphone working let's talk about acoustic phonetics we have plosives like t t c so your tongue hits somewhere and then a bunch of air comes out so we call these stops we also have fricatives so that's how you do it a little hole and we push air through it S S and F and we have nasals like n and M where we move air through our nose we have liquids for L and R we make a shape with our tongue and then we let the air flow around oh and then we have vocals where you just open up and let your vocal cords vibrate well, so let's walk, let's go, let's go, what have we got.
We have a liquid at the beginning and then we have a vowel and you think we have a stop with the T, but we don't say, let's say, like a person from Utah says. Mountain, it's just let there be no tea, then, God, my Boston accent is going to be very harsh here. H, yes. This part is a little safe. I appreciate the fact that pronunciation matters, which makes sense to him, but I think there are three places where these things matter, yes, const versus let. I think there are three areas where you'll think about these things: one is writing the code two is reviewing the code and three is talking about the code and you know what I'm going to give up, I think in case three, which is the case, Ryan spends a lot of time doing it.
I'm not saying this in an insulting way, I'm honestly getting it. Getting closer to this point, other than talking about reducing code costs and simply saying that it "makes life so much easier." In fact, I can see that and you can convince me of that argument by writing code. Maybe you can also convince me that writing code is simpler in the process of writing it if you only have one way of defining things, but between here reviewing the code is where I really think const helps a lot and I'm not the only one who thinks this is Malta, who is one of Google's assistant engineers in the past, who is now the CTO of its cellular team with Life statements, tells me that there will be a remapping change for the speed of understanding when reading the code, that It's the big problem here, that the object itself won't do it. be remapped in the same way when you see a pointer versus a double pointer, you don't have to be as thorough with the review, you know what you're doing, you have a value that it points to and you can change what it points to, but it can't be changed to. where it points is very important and someone in the chat also made a fair comment: don't forget to keep the code.
I would put it under review in a longer term sense. I almost put the review and maintenance here, which I think. it's just because going through and maintaining the code trying to figure out what this is supposed to do is huge and again, if we put something like let here, what I'll do immediately is when I'm reading this code or I'm dealing with this. in the future I'm going to try to find out all the places that touch this to make sure none of them are modifying it and if I have some crazy bug where Brands changes at some point during a render, now I have to go audit. each instance where this is used instead of the easy thing we would do here, which is find all instances of brands. push and if there isn't one in our codebase, it's probably okay now that you have to worry about it being assigned via equals, things get a lot more complicated very quickly, it's also a bit complicated if you're doing basic counter.
Example of component and I react like this if I make a new counter. TSX, everyone's favorite consumer feature counter. I could do this and believe me, it's tempting to make this an arrow function, but I'm going to resist the temptation and we have this function that counteracts the easy

mistake

that new developers will make, especially if they are I'm familiar with JS but don't react, this doesn't will work because we're not actually updating the state and if you were to fix that by making count equals count plus one, you'd immediately get an error that it can't be assigned to count because it's a constant. right, you shouldn't be able to assign the count because it's a constant, you should have to call set count, which you can fix very quickly by making these little things add up and makes it much more likely that you won't do them. types of errors both as an educator teaching someone the basics of something how to react also as a person who maintains this code over time or returns to it months, if not years, later and again, this should not be because const means constant, but because let means mutated.
Yes, when let is used, I know that the reason is because at some point the same thing is used, let tells me that this value that is assigned here can and will change, that is when let becomes useful because it is telling me by the way later either in this file or somewhere else this value can be reassigned, it sucks that you can reassign keys into a constant. I think you should have to clone the object yourself. With means that the thing is not reassigned, but it should mean that the thing is reassigned if the The only reason you use let in your code is because a reassignment occurs suddenly. let has a really powerful meaning of being aware of the same, being aware of where this changes.
I would say let is better and let is more valuable if you use const. because the reality is that you will have many fewer lets than consts. Here is the code base for the charging infrastructure. Let's take a look at all the let instances here. We have about 25 and a handful of these are also in copy, so I got about 23 total instances of let in the infrastructure codebase to load Thing versus 1239 for const, meaning these allow communicating something very specific which is very useful, very useful here we see that the URL is a window. origin plus pathname and if the search parameters are two search pams strings, we do more things in the URL in this condition or here we let Handler be the Hano Handler and replace this Handler with a Handler with a different version if It has a different version.
By putting let here, we indicate that this initial value will be different and reassigned as we go down. These are the things that let is really good for because it tells you very specifically that this is being remapped ideally and often directly below the problem. Isn't it that const means constant and we are not following the rule? The problem is the opposite, is that let should represent something very specific, which is what will change and if we use const, that allows us to use let in this way. I think there are three types of variables to say again there are three things, there are real constants, there are constant references and there are variable references, a real constant is a value that never changes, which in this case would be something like a string , so if we were to go back to the code like the const server URL is the same, sure, that's how it came out of the Json God super Maven package, very clever.
I think this is great, there are other types of constants you can have as well, some of them were shown there as costs Brands we work with and here I would really object. Freeze this because we want to be sure this never changes, but what if Brands can have things added and removed but we never reassign it to anything else at any time? Brands equals this, ideally, something like this, so I have the constant reference of Brands here where I clone this and now I have something new that I can modify do things for me indicates that I don't have to look for equals it's a constant reference this will always reference same thing most of the time this will be changed via some function somewhere but it's a const reference and I can't reassign it to something else to a different array.
I can change the array it points to, but this points to something. I saw there was a really good diagram here. In Josh W's comeo post, I was considering reading all of this, but work, I'll link to it in the description anyway because it's really cool if we define fruits with let and point here, we can change what it points to. I guess you clicked on these. Yes, we can change what the fruit points to because it's a let def definition, but if it's constant, we can't change it, it always points to this array, which is a significant difference, which is something different. should be treated as such, the problem I have with this talk andabout the hate of let is that it effectively pretends this doesn't exist, it pretends there's no need for a const reference to something, meaning there's no need for traditional pointers within C++, if you told any C++ developer no needs Star, you can just highlight the star or the pointers are fake because you can change the value it points to, you'd be laughed out of the CU room, that's a funny thing to say.
Does it make sense to call a pointer constant in C++ no, probably not. I don't think the name is good, but it's not a const value, it's a const reference, what it points to remains constant, so if that thing is one you can't change the string if that's in Array, you can make changes to the array but you can't change which array it points to. This is a significant difference and I don't know why we claim otherwise. I think the solution here is very simple. I think for actual constants we should do what is recommended, which is constant use of camel case.
I think camel case is a great solution when you want to have a real constant that can't be changed if you want to use a constant reference which is quite common to const some variable just constantly and if you want the variable itself to be able to change and reassigned let some variable. I think this convention makes the code very readable and makes things very clear, yes, it's not a camel case. This screaming case, there's another term for that, but yes, you get the idea, all caps for the actual constants, some constant variable without all caps for the constant references and then the variable references let some variables go crazy with a let.
I'm not against it, I just want to be very, very clear about the differences between these things because I feel like we pretend that part of this doesn't exist every time we get into these debates and I'm tired of pretending that constant references don't exist because there are so many reasons why I would like to point out something where what is underneath can change without changing what you point to in the first place; in fact you could say that most values ​​are that and this is again my problem. I think variable references where remapping is something that should be strongly indicative of that behavior and if you use let everywhere you are making let meaningless and I like a world where let means something which is possibly the funniest irony What all of this is about is that people who are really pretty and really anti-c const su The argument is that con is meaningless, but by pushing this argument they are making let meaningless.
I think let should have a meaning. I actually really like that let has a meaning and what I'm realizing now is that I'm actually one of the biggest fans of let which is why I think we should use const. The only reason you would say never use with is because you don't really respect let, so let me know what you think in the comments because I'm sure this is going to happen. To be spicy and until next time peace nerds, this was just linked in the chat and I have to add it right, it wasn't linked, it was mentioned that Dreambird has four types of variables, there are four types of constant declarations, the constants they can not. can be changed in any way so a const const cannot be changed a const v can be where you can make changes to the name so the variable can be changed but the variable name cannot be reassigned constants can be reassign but not edit so you can It doesn't appear or change the content of the name like you can't make Luke different, but you can reassign it to Lu, but variable variables can be reassigned and edited.
This is kind of how they work in JavaScript, where you can change it both ways. and you can also hit and do all of the following, where things get a lot more fun: immutable data, which we obviously want to make sure is properly immutable, mutable data is an anti-pattern, so if we use keywords with const to make a constant constant its value will become constant and immutable and will never change. Be careful with this keyword, it is very powerful and will affect all users globally. Wait forever. They have emojis for variable definitions. This might be my new favorite language.
What is this perfect? The programming language is the description here. I can confirm this is awesome so said look up the class name for maximum compatibility with other languages. Alternatively, you can use the class name keyword when creating classes, this makes things less complicated. The class names and the Dreambird keyword so you can't. use it instead, you can use the HTML class name, a tribute, what is this? This is awesome, it uses indexing based on negative 1, some language arrays start at zero which might be unintuitive for beginners, some arrays start at one which is not representative of how the code actually works dreambird does Lo arrays best of both worlds start in a negative one this is amazing holy I'm in love oh

If you have any copyright issue, please Contact