Check out a free preview of the full Functional-Light JavaScript, v3 course

The "Reducing Surface Area" Lesson is part of the full, Functional-Light JavaScript, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle explains that you should reduce the surface area where constants can be set by passing them in close to where the functions are declared.

Preview
Close

Transcript from the "Reducing Surface Area" Lesson

[00:00:00]
>> Kyle Simpson: Here's a different usage. You've probably seen functions used as values, here I'm returning a function back. So I have this function called addAnother, which takes in z as an input. Now it's not a constant in the global scope. It's a parameter that is passed in. And you'll notice on line seven, this little strange thing that's happening is that when I call out another the first time with the value one, that gets assigned to the z parameter, and I get back an addTwo function that is referencing z, right.

[00:00:34]
And then I execute that function that came back, which we know here to be called addTwo, I execute that with the second set of parentheses. They're in line seven. That second set of parentheses is executing that inner function that got returned back, and when that function runs, it is referencing a variable outside of itself.

[00:00:55]
That variable was not declared with a const, it's a parameter. But what's interesting about this code that was not true of the previous slide, is that this code, has a smaller surface area that we need to worry about, with respect to whether z gets reassigned or not. If we wanna ask ourselves is z in effect a constant, there are only two lines that we need to concern ourselves with, line one and a half and line two and a half.

[00:01:25]
They are the only two lines in the entire program that could possibly re-assign z.
>> Kyle Simpson: Do you see the difference between that and the previous slide, where there were 11 lines that could have theoretically have reassigned z? But because of the structure of this program, I've reduced the surface area where reassignment could occur.

[00:01:48]
And as a result of reducing the surface area, my argument is we're increasing readability. We are increasing the confidence for the reader of this code that when they see z passed in as one, they know it stays as one from then on. You with me? You had a question, yes?

[00:02:07]
>> Speaker 2: Yeah, would this be considered currying?
>> Kyle Simpson: That's a great question and we are gonna get to currying. So the question if you didn't hear it was, is this currying? It is in effect a partial application of a value, which you could also label as currying. But we wanna dig into that topic more, so hold on to that question till we get to that.

[00:02:28]
But yes, we are providing some input now, remembering that input until later. And that is in the general scheme of things, yes, partial application AKA currying. All right, so our inner function using that z variable, by the way int is using closure to do that, we'll come back to closure in our next section of discussion.

[00:02:49]
By remembering and using a z variable that doesn't change, it's in effect a constant. And therefore, both the outer function call the one with the first parentheses set, and the inner function, the one that is executed with the second parentheses set, line seven, both of those function calls are pure.

[00:03:12]
>> Kyle Simpson: That is to say, they have the predictability that we need and expect out of pure functions.

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