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. - วิทยาศาสตร์และเทคโนโลยี
"Side effect" is just a nerds way of saying 'this function changes some variables outside of the function'...sheesh.
Or interacting with a user in any way
I really appreciate your explanation. Now I've got a mental model of the operation of react's useEffect() hook.
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.
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.
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!
This really, REALLY helped me mate. Thanks a ton.
Great explanation, one of the best I have heard.
Nice Video Bro
Big Thank you, it helped alot, you are a life saver
Printf has a return value. In the video you said it doesnt.
cool
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
🤣🤣🤣