A different way of handling required arguments in javascript functions

Part of the Series:

#Did you know is a new series of Enmascript where we write short and concise explanations about topics we consider might be relevant or underestimated, if you are looking for more complete in-depth articles check out our other series.

Usually in javascript if you want to validate required arguments, you would do something similar to this:

function Person(water, food, candy) {
    if (!water || !food) {        throw new Error('water and food are required for Person');    }
    // Do something with water and food...
}

The constructor Person Above would throw an error if no water or food is supplied, this is a relatively common and popular way of validating parameters, but there is a different more functional way to achieve this.

Create a helper function that throws an error:

const required = name => {
    throw new Error(`Parameter ${name} is required`);
};

In the case above we are passing an argument name to the function because it feels more natural to print the name of the parameter that is required.

Then we use it like this:

function Person(
    water = required('water'),    food = required('food'),    candy
) {
    // Do something with water and food
}

What is happening here? How does this validation work? It is very straight forward:

If we don't pass the required parameter's values, the required function is executed throwing an error and forcing the user to pass each mandatory value everytime the function is invoked.

Passing the name of the parameter to the required function is only a preference; we can decide not to pass it and make the helper function simpler, it is a personal choice.

This approach is not limited to this implementation, we can use this same principle to create our own argument's validators and reuse in our functions to enforce consistency and security.

Do you have ideas that are similar or related to this topic? share them with us.

Want to leave a comment? Do it on twitter

Found something to fix in the article? Edit it and send a Pull Request on GitHub!

Want to be the first to receive our cool updates?

© Copyright EnmaScript 2021-2022. All code based on the MIT license.