Check out a free preview of the full Hardcore Functional Programming in JavaScript, v2 course

The "Pure Functions Checklist" Lesson is part of the full, Hardcore Functional Programming in JavaScript, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Brian reviews the checklist of what makes a mathematical, pure function, and asks the audience to determine if the examples presented are pure functions or not.


Transcript from the "Pure Functions Checklist" Lesson

>> So, a couple examples here. On the left, not a function, same input, getting different output there. Anybody know what, is it total, deterministic, side effects? Looks like it's not deterministic, because the same value gets different outputs. And on the right, it is a function, same values, same outputs.

Or same input, same output. This one's fun. [LAUGH] So we're gonna turn title into a URL friendly slug on the left, but it just throws an error in your face. So, you could debate whether that's a side effect, or that means it's not a total, or whatever else.

We're just saying, hey, if you end up with the URL that's less than 1, and where just gonna blow up. And on the right, even though it's got a Promise, and it's doing all this stuff, and it still has an error in there, we're not throwing the error, we're returning it as a value.

And that's perfectly acceptable. We're saying, this function returns a Promise, that returns either an error or the URL. Whereas the other one promised a nice URL, and then it blew up with an error. So that violates the mathematicalness of the function. And this last one, it's kind of a fun example.

So, sign up on the left, just, devil may care, saves the user into a database. And then we welcome the user in one way or another. We don't even know what it's doing. Can anybody even guess what's happening there? It's very side effecty. And then on the right, we actually return a function that will go do that.

[LAUGH] So it's totally pure, if I give it the same attributes every time, I get the same function out as my output, that will go do it. And this whole kicking the side effect down the road by returning a function, is a lot of the work arounds we do to keep everything nice and pure, and composable within our app.

And then we kinda push that under the collar, [LAUGH] you have to deal with the problems [LAUGH]. So, all right. We're gonna play function or no function. So, shout it out, is this a function? Anyone, Hank? [LAUGH]
>> No.
>> All right, it is not a function. If I gave it the same user more than once, I'm gonna get different outputs, right?

We're actually mutating the user's age. So, bizarre way to catch their age for the birthday function, but that's fine. All right, how about this one, is this a function?
>> Yes.
>> All right. That's right, same output, same input. And, no observable effects, and works for every possible string, I imagine.

How about this one? Anybody have an idea? So innocent. It's using snake case [LAUGH], that disqualifies it immediately. It's Python. I don't know, anybody?
>> I'm gonna go with no.
>> It's not a function-.
>> It's doing an external call, that's what I'm going for.
>> That's exactly right.

QuerySelector there is going to go out into the world and try to find this thing. Which, if I call it on a Tuesday at 5 o'clock it might work, and then Wednesday at 1 o'clock it doesn't work, different outputs, the same header_selector. So that will not work. Also, this one, I think there's two more of these.

[SOUND] [LAUGH] All right, anyone got an idea on this? Pure or impure? Is it a function or not a function?
>> Impure.
>> Impure, cuz of the location search.
>> That's right. So, again, different output. If the location search changes, this function, and you can notice there's no argument.

[LAUGH] So it's pretty. In fact, if I just made location search the argument, this would be perfectly functional, this would be a function if I just passed in the search.
>> So that's something to think about when you're creating functions is, is this function reliable? Or am I just doing things?

Am I making a mathematical function or not? Here we have a parseQueryString. This one's a trick line, anybody guess? We got vars, we got for loops [LAUGH], it is indeed a function. You don't have to read it, I'll just tell you [LAUGH]. Yeah, there's no, even if, look, we're mutating this thing within here.

But that's totally fine, the function itself within the boundaries of parseQueryString will always give you the same output with the same input. You got a question?
>> Are we leading into the closure being part of the definition? Whether your function is or isn't function?
>> That's a really good, that's a good question.

Let me hop over to this for a second. I just kind of mentioned this. So, if I do, my App, and it takes whatever, jQuery, or whatever else. And then, we're defining functions in here like, doThings. And, what's happening here is, and we'll return, doThings. All right, JQuery, the time of this function's definition can never change, right?

So, even though we're in a closure, it's very much like an object, and I could up here technically say, jQuery is null [LAUGH], and that would break everybody inside me. We're gonna, fast and loosely reasoning is morally correct, is the slogan here. Just define it, in terms of, in this environment can my things change?

Do I have changing variables, will this give me a different output for input? If it isn't likely, if that's not gonna be a case, then it's a function, don't worry about it [LAUGH]. That was a great question, a lot of people have that, and they're like, what? I think before we had an example where we say something like, the age limit is 18, and then we have a function that uses it.

And then we change it to 21 later in the future, or something. I mean, again, just use your judgment, if that never changes at the function, if it does, it's not. And you could pass it in, but I don't want you to have to jump through hoops that are unnecessary.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now