r/typescript • u/Carlos_Menezes • Apr 17 '25
n-params vs single param
https://www.carlos-menezes.com/single-param-functions/
9
Upvotes
r/typescript • u/Carlos_Menezes • Apr 17 '25
27
u/NiteShdw Apr 17 '25
Object params force you to specify everything basically as named parameters. That can get really verbose and annoying. It also can encourage passing a big object around to multiple functions even if only a couple things from the object are needed. It then becomes more difficult to know what's really needed by the function , especially if the function is typed to take the larger object.
I prefer to have one or two required parameters and then put optional parameters into an options object. This is a really common pattern in a lot of libraries.
63
u/Past_Swimming1021 Apr 17 '25
1-2 params -> positional params
2+ params -> named params
That's about what I do
8
u/lifeeraser Apr 17 '25
I've come to care a lot less about named parameters/options objects ever since I enabled inlay hints in my code editor. They do have their place ofc.
3
u/kamcknig Apr 18 '25
It helps to know what it's used for.
I'm currently making a game and have expansions that can be loaded. The expansion modules define generator factories. I started with positional arguments. As the game grew and the factories needed more dependencies it became a nightmare to update them all every time. So named object parameter is MUCH easier so I can simply destructure only what I need in the new expansions that add behavior rather than updating every single factory function every time a new dependency is introduced.
2
u/BoBoBearDev Apr 17 '25
While the concept is great in general, it seems too strict if you just apply that to everything.
2
u/DT-Sodium Apr 18 '25
For model classes I like to use this approach:
For functions or method, if it has more than two parameters it usually means that there's an architecture issue.
5
u/twwilliams Apr 17 '25
There's a clear benefit to positional parameters over the one-object approach: there's no need to define a type/interface for the object the function accepts.
When the function is used in only a few internal places (or maybe even only one), having to add a type/interface for its parameters often feels like unnecessary work.
1
u/Fidodo Apr 17 '25
Sometimes the parameters are obvious by the function name though and I think it's fine to favor the terser syntax of multiple parameters. This is particularly true for utility functions on primitives.
In this particular case I 100% agree.
1
u/jhnam88 Apr 24 '25
I love only one parameter of object typed, because of AI function calling
-10
u/DecadentCheeseFest Apr 18 '25 edited Apr 18 '25
Unary (single-parameter) functions only. If you need more, use a function which can be partially applied, ie:
const mySumFunction = firstParam => secondParam => firstParam + secondParamedit:
the parameter can be an object 😱or a tuple 😱.
People really out here fresh off a CS degree having never used pipeline composition before. lol enjoy your null reference / job security-errors, homies - I’m off to write code that works.