Function parameters are traditionally defined by position and we'll call this positional parameters.
Alternatively, we can gather the parameters into an object, which is unsurprisingly called parameter object.
Those who have used React are already familiar with parameter objects. React component props are parameter objects.
When should one use positional parameters or a parameter object?
A factory function is simply a function that takes in a set of dependencies and produces something.
For example, an user repository could be created by a factory function in order to offer a better interface over the raw db API.
But when factory function dependency list gets really long, it is a pain to keep the order of the parameters in sync with the call-sites.
One solution is to introduce a parameter object.
Life is good, parameter objects saved the day, but then one introduces TypeScript into the mix.
Typing parameter object is possible with TypeScript, but it can get annoying quickly.
For every parameter, we have to write out the name twice.
But why were we using parameter objects in the first place? One of the reasons was we couldn't detect the error of accidentally swapping two parameters without running our code. But that problem no longer exists in TypeScript! TypeScript knows what is the expected type of each parameter. We can go back to using positional parameters.
This is why it is important to understand the original problem solutions intended to solve. New tools, techniques and even time change the context in which problems can be solved. New and better solutions can be found if one understands the original problem.
Do you see beyond solutions to the original problem? We like to hear from you, Battlefy is hiring.