Side Effects4:45 with Craig Dennis
Side effects in English usually represent something you didn't intend to happen, the definition is a bit more strict in Functional Programmingville.
In an effort fo keep our functions pure, 0:00 we need to make sure that they are free of side effects. 0:02 In English, a side effect is something that happens when you didn't intend it to. 0:06 For instance, 0:10 a side effect of eating a taco might be that you got a salsa stain on your shirt. 0:11 Now, you didn't intend it to happen but it did and boy, was that taco good. 0:16 So typically, we think of a side effect as something that we didn't intend to happen. 0:21 Now, unfortunately, the saying is a little overloaded. 0:25 Here in functional programming land, a side effect refers to anything outside of 0:29 the function scope that gets modified, even if you intended to do so. 0:33 It's still called a side effect. 0:39 If it helps, I remember this discrepancy by calling it a out side effect. 0:41 For instance, if we write a function that updates a list that is outside of it's 0:47 scope even if I intended to do so, it is still considered a side effect. 0:51 This out side of it's scope holds true for all IO, or Input Output. 0:56 Now, reading from a file is off limits, because that file could change, 1:01 and therefore, 1:05 would make the function behave differently depending on the state of the file. 1:06 Where adding to a file is against the rules as well. 1:10 Pure functions are meaning you should be able to call them multiple times. 1:13 And it won't affect anything. 1:19 This is why output is bad news. 1:20 Also, remember that one of the goals of pure functions is that they can 1:23 be memoized. 1:26 So the code doesn't need to run anymore. 1:27 After it knows the result so 1:30 if you were relying on your function to output to a file and it was memorize, 1:32 that output wouldn't happen because the function actually isn't running. 1:36 It's just using that stored value. 1:40 So the same goes for system on out. 1:43 Writing to that is a side effect. 1:45 That is why we were doing that inside of the four each method. 1:47 ForEach, takes a consumer and a consumer remember that takes a value and 1:51 returns nothing. 1:55 Consumers are kind of built for side-effects, 1:56 I mean what good is a pure function that returns nothing right? 1:59 It has no other way to do anything. 2:01 So when you see a consumer used it is always gonna be not pure function. 2:04 There is one more handy function that I'd like to show you that is intended for 2:09 you to use to produce side effects and it can be used in side streams. 2:13 It's called peek. 2:17 Here, let's go take a peek at it in use. 2:18 #badjoke so I was at a meet up and somebody was telling me about their 2:20 office and where they worked and they know that their company started with an n and 2:26 I can't remember what it is, so let's do this. 2:30 Let's just filter these companies, and 2:33 remember these are just the company names up here, these distinct company names. 2:35 And I want to filter those, so let's do that. 2:38 So let's say we have a company stream here and I want to filter, and 2:42 I know that it's the company and know that it started with an N, so let's do that. 2:47 Let's start with N here. 2:54 And then I'll use this side effect, right, the consumer here for each. 2:57 And again, it's a side effect because it is not returning anything so 3:03 the only thing it can do in side effect of printing out right, Okay. 3:07 So let's run that. 3:14 That's all? 3:18 I think maybe we should peek just to make sure. 3:21 Now, I'm gonna do this. 3:23 I'm gonna add this to the stream I'm gonna come in here and I'm gonna peak 3:24 because I'm pretty sure that, maybe it was something that sounded like an n. 3:29 We'll do company and I'll just print out, 3:34 and I'm gonna make this big arrow thing here. 3:38 So that in the output we know something fat arrow that we're debugging here, 3:41 and we'll add the company there. 3:46 Notice that this took a consumer, so 3:48 we know that this is side effect friendly, okay? 3:50 So here we go, so 3:53 now when I run this, you'll see that I've printed out everything else. 3:55 The n's are still in there but 3:59 we also printed out everything that doesn't start with an n. 4:00 And this allows for handy debugging, but don't forget to remove them. 4:03 So with that, I think we can cross some items off of our parking lot. 4:09 Let's switch over to there. 4:12 I think we've got our pure taken care of. 4:15 Let's strike that through, and I think we just finished our side effects. 4:18 Don't forget, outside effects. 4:22 So these methods on streams like Peak, they're super handy, aren't they? 4:25 They accept functions are arguments and they really let you be more declarative, 4:30 as well as not coding yourself into a corner. 4:35 Now when this happens, it is known as a higher order function. 4:37 And we can actually build these ourselves. 4:41 Let's do that after this quick break. 4:44
You need to sign up for Treehouse in order to download course files.Sign up