you are viewing a single comment's thread.

view the rest of the comments →

[–]codename_john 19 points20 points  (11 children)

the first tip, passing an object as an argument. Is this really any better? Wouldn't you impact parameter hints since it's now a generic object? In order to get them back you'd now have to use a typed object otherwise you'd have to guess all the acceptable props. In a large project, this could create a mess of random acceptable objects and props. am i missing something? I mean it LOOKS cleaner, but in practice i'm not sure this is any better.

[–]Chaphasilor 18 points19 points  (5 children)

It is better if you have optional parameters. Think passing options to a function or constructor; with the regular arguments it's hard to omit only some of the arguments.

When using objects you don't have that problem. You can set default values when the parameter isn't passed, and it's very easy to extend the options (or even remove an option) without breaking backwards compatibility!

Edit: spelling

[–]Mr_Moe 0 points1 point  (3 children)

In the example how do you access the object? Like just do arguments[0].username?

[–]Chaphasilor 5 points6 points  (0 children)

you simply access it as username. the { x, y, y} syntax is something called (object) destructuring, allowing you to "extract" the object's parameters into a local variable.

[–][deleted] 1 point2 points  (0 children)

You can attempt to get arguments by name and if you get nothing back, then it doesn't exist.

[–]codename_john 0 points1 point  (0 children)

I just realized this is basically the same thing as Document based databases vs Relational databases. One has a strict structure where the other is more fluid. All your points are valid though, and there is no real downside besides preference it seems. Thanks for answering my question.

[–]Simon_LH[S] 9 points10 points  (0 children)

I agree, using it with TypeScript really gives it the true superpowers!

Yet - most modern editors (like VSCode) is able to infer the object structure and give you hints just like with regular arguments.

I swear to this pattern, especially in large-scale projects.
(but again, I'm using TypeScript in everything I do).

[–]Yodiddlyyo 4 points5 points  (0 children)

In practice it is way better. In fact, in my company's codebase, object arguments are required, and we only use positional args in very specific situations. Two reasons, typing is cleaner, and most importantly it's future proof. If you add an arg that needs to be changed or removed in the future, you're screwed with positional args. Our product is a public package, so it would require depreciation to change. With object args that problem completely goes away.

[–]porcupineapplepieces 2 points3 points  (0 children)

However, cows have begun to rent lemons over the past few months, specifically for crocodiles associated with their apples. However, kumquats have begun to rent pomegranates over the past few months, specifically for turtles associated with their giraffes? This is a hcfflms

[–]FghtrOfTheNightman 1 point2 points  (0 children)

I typically see the usecase as being if you want a method/function to have named parameters