Это видео недоступно.
Сожалеем об этом.
Pure Functions Javascript - A Way to Avoid Side Effects in Javascript
HTML-код
- Опубликовано: 18 окт 2021
- Learn what are side effects in Javascript and what are pure functions. It's important to write mostly pure functions and avoid side effects and mutations. This makes your code more stable and robust. You will see on the real example 2 rules of pure functions and why they are important.
► CHECK MY COURSES - monsterlessons...
FOLLOW ME
► TWITTER - / monster_lessons
RECOMMENDED VIDEOS
► My editor setup for web development - • Best Text Editor for W...
► Angular Tutorial for Beginners - • Angular Tutorial for B...
► Vue JS Crash Course - • Vue JS Crash Course fo...
► React Hooks Full Course - • React Hooks Tutorial f...
► Typescript Course for Beginners - • Typescript Crash Cours...
► Build a Todo App with Angular - • Build a Todo App With ...
► Creating custom select library - • Custom Javascript Drop...
► HTML Price comparison - • Practice CSS and HTML ...
► How to build Quiz with React hooks - • How to Build a Quiz Wi...
MY COURSES
► NestJS course - • Next JS for Beginners ...
► Docker + Docker compose course - • Docker Compose Tutoria...
► Angular + NgRx course - • Angular Course 2021 - ...
► Vue + Vuex course - • Vue Course With Projec...
► React hooks course - • React Hooks tutorial b...
Great vid! However, there are some things that I would like to address. I'm sorry, but what you did with that function is a no go in most languages. By making functions that are supposed to be impure pure, you are violating lots of practices.
- One, allocating memory for an entirely new object, where state of one could just be altered. Be mindfull of your memory, and be mindfull of informing the user that you are copying stuff. Yea it's Javascript and that tends to happen less then one might think, but it's still something to keep in mind. This goes on to be supported by number two.
- A function that describes to alter state, is allowed to alter state. Your function is called addItem. It should not just return a new object. In this case you inform the user of this function that it alters the state of a variable, your array. If I expect that to happen and it doesn't, then we've got ourselves a big problem. Always inform the programmer when you are returning a new object instead of the passed one.
- And third, use constants and readonly variables that can't be altered in any way shape or form. Don't expose functions publicly, that can directly or at all alter state. For an array, you can make it immutable whenever you pass it.
Great vid tho, good solid basis. Just don't forget to mention that one can write a pure function more easily if they use immutable parameters, and keep an eye on the function context.
Thank you for such a big comment. I agree with what you said but related to normal languages where you have constants, allocating memory, etc.
But in js everything is mutable, arrays are mutable (unless you freeze them). You don't have readonly properties in language at all. And you don't have immutable parameters if they are arrays and objects.
Great tips!
Glad it was helpful!
Спасибо, освежил в памяти. Как насчет понятия детерминированности функции? Тоже ведь из этой оперы.
Deterministic functions are functions which return the same result if they are called with the same arguments. This is exactly what this video is about.
Thank you for posting this 🤗
You're so welcome!
thanks 😊
you have a great channel
Welcome!
hi there, in your last example, cart.items is not an argument, even though cart is an argument, that is why it is not pure?
It is not pure because you pass arrays and objects by reference and not by values so updating them leads to updates elsewhere.
@@MonsterlessonsAcademy thanks! I love this tuto, do you have anymore of such videos, i thought a few functions is hard to understand when it comes to ecommerce.
Super, I would like to know what are the benefits can I get if I joined (membership)
Hi there are no benefits on youtube membership now except of the icons near your nickname. It is just if you like the content and want to support me.
@@MonsterlessonsAcademy yes