Eliminate Anonymous JavaScript Functions with Pointfree Programming

Kyle Shevlin
InstructorKyle Shevlin
Share this video with your friends

Social Share Links

Send Tweet

This lesson teaches you what pointfree programming is, passing a named function as an argument to avoid writing an anonymous function with interim variables instead. Pointfree programming increases code legibility, decreases the surface area for bugs, and makes our code more composable and unit testable. We will demonstrate this by removing the anonymous arrow function passed as a callback to a map method with a named function.

Kyle Shevlin: [00:00] Often, when we pass functions as arguments into other functions or methods, we use anonymous functions with interim variables. If we consider the map method, you might see something like this in order to double the numbers of our array.

[00:16] While this works, this actually leaves us a lot of surface area for bugs and misunderstanding. X acts as an interim variable. It is a placeholder for our data. We can just as easily name it foo, bar, or Y. None of those tell us much about what the data is or even what we are doing to the data.

[00:39] Pointfree programming is when we remove those anonymous functions and interim variables and instead pass in named functions directly into other functions. This works by pulling the anonymous function out and making a named function with it.

[00:53] In this case, we'll make a double function that receives any value and returns its double. Then we can pass double directly into array. What we gain from pointfree programming is legibility.

[01:07] It's really easy to read map and double and know that each item is being doubled. We reduce the surface area for bugs by not introducing new interim variables. We gain the ability to unit test our named functions.