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

Поділитися
Вставка
  • Опубліковано 30 лис 2024

КОМЕНТАРІ • 17

  • @MonsterlessonsAcademy
    @MonsterlessonsAcademy  2 місяці тому

    WATCH NEXT: Javascript Interview Questions and Answers - Dominate Your Next Interview - ua-cam.com/video/wnYKH2dO620/v-deo.htmlsi=5DfbGEfhXWiiv0a_

  • @LordErnie
    @LordErnie 2 роки тому +2

    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.

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  2 роки тому +1

      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.

  • @IkraamDev
    @IkraamDev 3 роки тому

    Great tips!

  • @Himanshuyadav-us6me
    @Himanshuyadav-us6me 3 роки тому

    Thank you for posting this 🤗

  • @Blue-bb9ro
    @Blue-bb9ro 3 роки тому

    thanks 😊
    you have a great channel

  • @ilyaredkin4172
    @ilyaredkin4172 3 роки тому +1

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

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  3 роки тому +1

      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.

  • @JJ-ot3ps
    @JJ-ot3ps Рік тому

    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?

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  Рік тому +1

      It is not pure because you pass arrays and objects by reference and not by values so updating them leads to updates elsewhere.

    • @JJ-ot3ps
      @JJ-ot3ps Рік тому

      @@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.

  • @anurap.kasthoori6741
    @anurap.kasthoori6741 2 роки тому

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

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  2 роки тому

      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.

    • @anurap.kasthoori6741
      @anurap.kasthoori6741 2 роки тому

      @@MonsterlessonsAcademy yes