Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Eliminate Anonymous JavaScript Functions with Pointfree Programming

    Kyle ShevlinKyle Shevlin
    javascriptJavaScript

    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.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Kyle Shevlin: 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.

    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.

    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.

    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.

    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.