Video Summary and Transcription
Let's write Node like it's 2022. We can now iterate over objects more easily with the for-in loop. Template literals make string concatenation and multi-line strings much simpler. The structured clone feature allows us to clone complex objects without the need for hacks like stringifying and parsing. Default parameters in JavaScript are now much more convenient. Async await eliminates the need for callbacks. Arrow functions inherit the scope, eliminating the need for 'this equals that'. Const prevents variable reassignment. Let helps catch errors earlier. Write code like it's 2022!
1. Introduction to Writing Node in 2022
Let's write Node like it's 2022. My name is Pedro, a software engineer at blockchain.com. I've been evolving with Node and I'm proud of it.
Hi. Let's write Node like it's 2022. Well, first of all, who am I? My name is Pedro, and I'm a software engineer at blockchain.com. And, well, I was looking at the code I wrote a few years ago, like 10 years ago, and I saw that my life was very, very challenging and fun. But nowadays, it's just fun. I wrote a bunch of this equals that, and I used a lot of low dash underscores, and everything else that you can think of. Just make some stuff that work as expected today. Node evolved, and I've been evolving with it, and I'm very proud of it.
2. Writing Node in 2022
Let me tell you how to write code node like it's 2022. We can now iterate over objects more easily with the for-in loop. Template literals make string concatenation and multi-line strings much simpler. The structured clone feature allows us to clone complex objects without the need for hacks like stringifying and parsing. Default parameters in JavaScript are now much more convenient.
So, let me tell you how to write code node like it's 2022. Look at this example, for example. So, we had to use multiple methods to iterate over an object. So, in this get total score, we had to use object keys and a for each to access the property inside of the object, and then assign the value to the totals. But now, we can do a for in, and we can access the key of the object And we can do it very easily, and with only one method. So, it's way easier to read, it's easy to return about. I know what I'm reading as soon as I put my eyes on it, way better.
Well, we had to concat strings to get phrases with variables, and LongAVI started with multi lines. These were very, very hard to create. In this example, I just created one that puts together three strings in this case. I had to use strings to spaces because I hated to add space in the end of this one, and here, I really need it because I can't add anything to this string directly. But now, I can use template literals and I can just make it work. I can do multi lines easily and everything I see is exactly what appears in a screen. So, much easier.
We had to use the hack to stringify and parse objects to clone them. And this wasn't a case for simple objects. You could do other stuff. And in load dash we had clone deep that made exactly the same thing as this code does. That is, basically get an object with complex and nested data and put it in another variable. As JavaScript would use a pointer to this one. If I only get like B equals A, I would get the value assigned as just a pointer to A and not actually a value of A. So I had to transform it in a string and then parse it to actually clone the value of the object. Well, that's a very complicated explanation to just copy an object. But now we can use structured clone. That is exactly what it says. It's a structured clone with all the properties, nested ones, and it just works as expected. I mean, it's very nice.
What about the default parameters? Oh, this one. Everyone that writes JavaScript in 2022 knows that the default parameters are very, very nice. Back in the day, you had to in the top of every function, you would have the variable equals variable or something, just like here.
3. Writing Code Like It's 2022
A equals A or 1. You can pass default parameters right in the function. Async await eliminates the need for callbacks. Arrow functions inherit the scope, eliminating the need for 'this equals that'. Const prevents variable reassignment. Let helps catch errors earlier. Write code like it's 2022!
A equals A or 1. And in some cases, in cases of objects, you would have a bunch of them listed at the top of the function, making it very hard to read. But today is not the case. Because today, you can do it like A equals 1 and B equals 2 in the top.
So on the function, you can literally pass the default parameters right there without any other fuss. And Nogami started with async await. Now you don't need callbacks to handle HTTP requests. You can use async await. I'm an async await person but you can use like then that is very helpful to write like async code. You have arrow functions that don't have this scope and they inherit the scope. So you don't have to write this equals that in a bunch of cases. You have const that doesn't let you reassign the variable. You have let that makes it easier to see the errors before you see them because with the var it would hoist to the top and you would get a bunch of cannot read a often defined for example. And there are other things that I don't remember right now but are very very important.
So well do a favor to everyone and write like a 2022. It's not that hard. Well that's my GitHub and my Twitter. And that's all folks. Let me know if you have any questions.
Comments