This course has been updated! We now recommend you take the Hardcore Functional Programming in JavaScript, v2 course.

Check out a free preview of the full Hardcore Functional Programming in JavaScript course:
The "Recognize Pure Function" Lesson is part of the full, Hardcore Functional Programming in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

A function that does not change anything outside of itself is considered a pure function. Pure functions react the same way to the same inputs. They are more testable, portable, memoizable, and parallelizable. Joe plays a game called Pure or Impure to help the audience recognize pure functions.

Get Unlimited Access Now

Transcript from the "Recognize Pure Function" Lesson

>> [MUSIC]

>> Joe Nelson: And the next bit is recognizing these types of functions that are really useful, like the days in month as a pure function. A function that doesn't change anything outside of itself and reacts the same way to the same inputs, they're called pure, and what does it make them?

[00:00:21] Well, we've just been beating it into the ground. It makes them more testable, as Brian mentioned, it makes them more portable in the case that they don't actually depend on some browser thing. You could use them on the client in the server. It makes them memorizable, and memorization is a technique that once you run a function one time with a certain input, a little thing watches over and says okay, I'll remember that.

[00:00:43] Next time someone tries to call this function without input, I won't even ask the function, I'll just give it the input. And like in Haskell and in other places, and I guess, theoretically, in JavaScript too, you could memorize these things without having to think about it. So in the cases where you wanna make that space versus time tradeoff in your programs, you don't have to change, your function's already pure.

[00:01:05] And finally parallelizable, the tongue twister word, so [LAUGH] because the things don't change anything, it doesn't matter which ones you're running at what time. If you have different processors, you can run as many as you want. They're not gonna step on each other's toes. So do we really understand what purity means?

[00:01:30] Let's play pure or impure. I guess we have an advantage here with the people that are present because we can shout it out. So here's a function, getQueryVariable. You give it a variable, and the idea is, if I was in a page like items question page equals three, and I say give me the page, like the query string parameter, it should give me three.

[00:01:58] So I give it the variable. It checks and it gives me, pulls it out of the url and gives it to me. What's your guess? Pure's good, by the way, so I'll put thumbs up for pure. Kill him for impure.
>> Joe Nelson: You're feeling thumbs down? Why do you feel thumbs down about this one?

>> Speaker 2: I don't wanna be the one to answer that. [LAUGH] Does anybody else want to-
>> Speaker 3: Windows location.
>> Speaker 4: Windows locations then?
>> Joe Nelson: Yep, it's looking, it's looking at things it shouldn't look at. You're not pure. Next, okay, here's one, random numbers. This thing generates random numbers. Who knows what comes out of that?

[00:02:38] We give random numbers, like these seed values, to kick it off, and it gives you a random number, kind of complicated in there. What do you think about this one? You're happy about it. So you're not happy about it? A lot of happiness. It's making us all joyful.

[00:03:01] It's pure, because although it's doing complicated things, it's doing them in a well defined way. It's just like some number crunching, and you give it the two numbers. And it's kind of crazy as a side note that this thing actually generates fairly statistically spread out numbers based on what you put into it.

[00:03:17] I was playing, nudging them by one or two, and I was getting drastically different numbers, it's a bizarre algorithm. So pure or impure, the final round, chattyAdd. It adds numbers, but you kind of wanted to know what's going on because you're balancing a checkbook or something. So it's telling you what just happened, so you know what's going on.

[00:03:39] Feels good? You gotta feel like Caesar here. I wanna see more thumbs, should this thing live or should it die? Live. If I set a really pure function, one that we can really count on, it could be parallelizable, I got it that time. And it doesn't matter when they're happening, the result will look the same from the outside world.

[00:04:04] And if this thing's logging and if you ran it in parallel, you wouldn't know which order the logs might come in. So, it's not pure, it's changing something. The log, it's something, it's out there. It's outside of the function, and it just went out there and did it.

>> Speaker 2: People in chat are kicking butt. They're all like, I know, impure, done. [LAUGH]
>> Joe Nelson: We got the pros.
>> Speaker 2: Yeah [LAUGH].
>> Speaker 5: Although wouldn't it matter what console actually does? You're assuming you know that console is a impure function.
>> Joe Nelson: That's a good point. It's like dependency injection, yeah.

>> Speaker 5: Yeah.
>> Joe Nelson: Maybe the takeaway would be that, if you compose pure functions, it stays pure. But if one function ever relies on another, it'll become impure as its argument was impure. So purity begets purity.
>> [LAUGH]
>> Speaker 5: So would this function actually be pure, what you're arguing is it's the input that makes it impure.

>> Joe Nelson: Yeah, you're right.
>> Speaker 5: Cuz you're assuming console is impure.
>> Joe Nelson: You're right.
>> Speaker 5: Okay.
>> Joe Nelson: I guess I assumed it was in a browser, and it passed in the console, but it doesn't have to be the case. It could be something that just throws it away, it would be very impure, very pure.

[00:05:12] It gets your computer hot or whatever.
>> Speaker 5: Yeah, it warms it up. Well, I don't necessarily know what console log does.
>> Joe Nelson: Yeah, exactly, it could be something innocent as a flower.
>> [LAUGH]
>> Joe Nelson: So you just played our game