Pure Functions Javascript - A Way to Avoid Side Effects in Javascript

Pure Functions Javascript - A Way to Avoid Side Effects in Javascript

Monsterlessons Academy

2 года назад

3,471 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@JJ-ot3ps
@JJ-ot3ps - 14.02.2023 06:57

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?

Ответить
@LordErnie
@LordErnie - 26.10.2022 01:46

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.

Ответить
@anurap.kasthoori6741
@anurap.kasthoori6741 - 10.09.2022 02:34

Super, I would like to know what are the benefits can I get if I joined (membership)

Ответить
@Blue-bb9ro
@Blue-bb9ro - 15.11.2021 07:48

thanks 😊
you have a great channel

Ответить
@ilyaredkin4172
@ilyaredkin4172 - 10.11.2021 08:06

Спасибо, освежил в памяти. Как насчет понятия детерминированности функции? Тоже ведь из этой оперы.

Ответить
@Himanshuyadav-us6me
@Himanshuyadav-us6me - 20.10.2021 06:00

Thank you for posting this 🤗

Ответить
@IkraamDev
@IkraamDev - 19.10.2021 19:56

Great tips!

Ответить