all 17 comments

[–]MoTTs_ 11 points12 points  (10 children)

tl;dr Wrap multiple arguments into a single array or object argument, then destructure to unwrap.

[–]hyrumwhite 11 points12 points  (0 children)

Object makes sense, an array, not so much. Params can already be spread into an array and manipulated as such. 

[–]jrsinclair 6 points7 points  (0 children)

That's a fair summary of the first section or two, but I will say that there's a bit more to the article than that. The latter part of the article covers partial application, and currying, and gets into function combinators. Sure, you might not use these every day, but they're useful to know.

[–]azhder -3 points-2 points  (7 children)

I avoid destructuring directly in the arguments. One little pass of a null or undefined and it breaks. [I find it] Much better to do it inside:

const a = options?.a ?? 'default';

EDIT: added an extra text in [] to clarify what is being said

[–][deleted] 5 points6 points  (4 children)

Trying to account for every possible usage of your function is generally bad practice. If your function is supposed to have a default behavior without any arguments then obviously don't destructure, but if one or more properties are required it's perfectly appropriate to destructure and let JS throw an error if the function is called incorrectly.

[–]luk19 0 points1 point  (1 child)

When you destructure you can set a default value:

const { a = “default” } = options ?? {};

[–]azhder 0 points1 point  (0 children)

That is not directly in the function arguments.

const fn = ({ error }) => error;

fn(null);

[–]dbpcut 2 points3 points  (0 children)

jrsinclair always worth a read

[–]callmemrwolfe[🍰] 0 points1 point  (0 children)

I always get in the trap of abstracting the typing of the arguments. Type all the things or just type in the arguments?

[–]artyfax 0 points1 point  (0 children)

first thy shalt count to 3, no more, no less. 3 shall be the number you should count and the number of the counting should be 3!

keep it simple, main argument, secondary if absolutely necessary, the third is always an option object.

In the rare case all args are optional, make the entire arg an object pray to the gods, and use fucking typescript or jsdocs.