What are side-effects?

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ก.พ. 2019
  • In functional programming, people often use the term side-effect. But what does it mean? Side-effect is any external effect a function has besides its return value.
    ►► Audio, Video, and Transcript available: lispcast.com/w...
    ►► Subscribe on iTunes: itunes.apple.c...
    Transcript
    What is a side effect? Hi, my name is Eric Normand. These are my thoughts on functional programming. If you like what you hear, please hit subscribe.
    What is a side effect? This is a very important concept in functional programming. Side effects are anything that is outside of the scope of a normal pure function, otherwise known as a mathematical function.
    If you remember from high school algebra, a mathematical function maps the domain to the range. It maps the arguments to the return value as we would say as programmers. It is a mapping that is timeless, meaning it doesn't matter when it's run.
    It's not having an effect on the world. It's a mapping just by definition. It just maps this to that. We do that a lot with our functions. We write a function that takes X and doubles it, and it returns that doubling.
    Every time we call it with three, we're going to get six, every time. It's not dependent on anything outside of the argument that we pass it. Imagine a function that has some other influence over that answer.
    Or imagine every time we call this function, something happens in the world. You could imagine that number six getting written to a file or getting sent over the network stored in a database or logged to disk, printed onto the screen.
    Now, it's not timeless. Now, it's not really this relationship between inputs and outputs. It's a relationship between inputs and outputs with something else happening, which is called the side effect.
    You call this to get the number six out, but something else happens that maybe you didn't know about, maybe you didn't want it to happen, or maybe that's exactly what you wanted to happen. You don't even care about the return value.
    In functional programming, we call that a side effect. It's a side effect as opposed to just an effect. The term is used loosely. As these terms go, it's complicated. The term is used loosely, but in general it's used to say that this isn't the main reason to call this thing.
    The main reason to call double is to get six out when you pass in three. This other thing happens, too, so it's a side effect. You could call them effects. If you have the print function that prints to the console, it doesn't have a return value really.
    It's really weird to call it a side effect, but people do. They use the term loosely, like I said. In functional programming, we try to isolate those effects. Separate them out from the mathematical functions.
    Double is a perfectly good mathematical function. You could write it once and use it for the rest of your life. Maybe you want to optimize it or something, but it's going to work forever, regardless of context.
    It's something that you could put into a utility library and basically never touch again. Your context of where things are running, maybe you don't want to print it, maybe your log system changes, maybe the database you're going to store it in changes, all that stuff is way less timeless than this relationship between the argument and the output.
    There's a good reason to separate that out. Timeless stuff, it changes less often. It's easier to test. It's easier to debug. Imagine testing this thing. If you're going to test a function, you're going it run it multiple times with different inputs.
    You're going to test easy cases, some hard cases. Maybe you just put it through its paces and you test a hundred, a thousand cases. Imagine if it was writing to disk or hitting the database.
    If it's hitting the database, you have to make sure the database is running. If you are writing to a file, you got to make sure the file exists and is open. Your test become a lot more complicated.
    Whereas this easy return value function, it's all it does. It's all in outputs. That's so easy to test, so easy. That's one of the reasons why we like to isolate them. The hard stuff in programming where most of our bugs come from is all having an effect outside of itself.
    All the writing to disk, the sending a message over the network, the reading from the database, the drawing stuff on the screen, all of that stuff is hard. Responding to user input. This is where the complexity lies.
    As much as possible, we want our code to not depend on that. You need to do all those things. That's why we run our software but if you can isolate those things and manage them...You isolate them so that you can manage them. Whereas all this other stuff that's just calculations, that's just functions in the mathematical sense, all of that stuff doesn't need managing in the same way. It's way easier.
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 14

  • @sawilliams
    @sawilliams 5 ปีที่แล้ว +18

    "Side effect" is just a nerds way of saying 'this function changes some variables outside of the function'...sheesh.

    • @ahmeeeeeeeeeeeed
      @ahmeeeeeeeeeeeed 3 ปีที่แล้ว

      Or interacting with a user in any way

  • @mikekwame4932
    @mikekwame4932 ปีที่แล้ว

    I really appreciate your explanation. Now I've got a mental model of the operation of react's useEffect() hook.

  • @eligolin9947
    @eligolin9947 ปีที่แล้ว

    A main difference between 'side effects' and effects, is that the latter is controllable by us. By controllable I mean that we can separate between the declaration of the effect and its execution.
    So an effectful function which prints to console returns a value which encapsulates the 'ability to print', that is opposed to a side effectful function which just prints something to console the moment you call it.

  • @pelletey
    @pelletey 5 ปีที่แล้ว +1

    Thanks again Eric, your audio quality is a lot better now. It's interesting how these side effects are also the best things to mock out when it comes to testing.

  • @johnmarkperocho2590
    @johnmarkperocho2590 5 ปีที่แล้ว +4

    Nice topic on the side effects. I suggest showing an example in code to emphasize the theory, this way nubes like me and for those people that have a limited understanding in the English language will have a solid view and be able to apply it in real projects. Nice explanation by the way. kudos!

  • @hi3694
    @hi3694 3 ปีที่แล้ว +1

    This really, REALLY helped me mate. Thanks a ton.

  • @ironmagma
    @ironmagma 4 ปีที่แล้ว +1

    Great explanation, one of the best I have heard.

  • @bharatkafle9462
    @bharatkafle9462 ปีที่แล้ว

    Nice Video Bro

  • @mailtobasit74
    @mailtobasit74 4 ปีที่แล้ว

    Big Thank you, it helped alot, you are a life saver

  • @prakhargupta2960
    @prakhargupta2960 3 ปีที่แล้ว

    Printf has a return value. In the video you said it doesnt.

  • @ProfessorSauvaje
    @ProfessorSauvaje 3 ปีที่แล้ว

    cool

  • @letsexplore2093
    @letsexplore2093 5 ปีที่แล้ว +4

    Brother you are not giving any political speech you are explaining a one programming language concept so please so stop making such making useless video and use computer and example