TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

N-Params vs. Single Param

37 pointsby carlos-menezes28 days ago

11 comments

disillusionist28 days ago
I started using this pattern years ago and haven&#x27;t looked back. React components are defined using a single properties param and it feels natural to extend the practice to other methods and helper functions that I write.<p>One nice unexpected side effect is that I end up with more consistency in variable naming when using a param object in order to benefit from object definition shortcuts.<p>ie I will usually write something like<p><pre><code> const firstName = getFirstName(); doTheThing({ firstName }); </code></pre> rather than<p><pre><code> const fName = getFirstName(); doTheThing({ firstName: fName });</code></pre>
评论 #43721066 未加载
评论 #43720804 未加载
hwpythonner28 days ago
Not an expert in JS, but maybe this makes sense in JS where everything is passed as objects anyway and having a single param is often more readable there.<p>But in native languages like C&#x2F;C++&#x2F;etc, this pattern usually hurts performance. Separate arguments go into registers (faster but depends on number of arguments), while structs often involve indirection, and sometimes alignment&#x2F;padding issues. Also, passing a const struct doesn’t guarantee the fields inside are truly const.<p>That said, it&#x27;s context dependent. If you&#x27;re passing stuff through multiple layers or want a more stable function signature, using a struct can be cleaner. Just not something I’d generalize to any language or any use case.
评论 #43720989 未加载
评论 #43721279 未加载
评论 #43722639 未加载
评论 #43723382 未加载
sixo28 days ago
A little while ago I sketched some ideas for a programming language [1], one of which was that I would like a language who did not distinguish between these two cases. Obviously you can always write a function with a single N-param argument, but it&#x27;s rather verbose, but would be nice to also be able to write the param list normally and then refer directly to its parameter type elsewhere. Although this would require that the &quot;set of possible parameter lists&quot; were the same as &quot;the set of possible object type&quot;, which isn&#x27;t the case in any language I know of.<p>[1] <a href="https:&#x2F;&#x2F;samkrit.ch&#x2F;2025&#x2F;03&#x2F;09&#x2F;programming-1.html#anonymous-type-definitions" rel="nofollow">https:&#x2F;&#x2F;samkrit.ch&#x2F;2025&#x2F;03&#x2F;09&#x2F;programming-1.html#anonymous-t...</a>
评论 #43722713 未加载
评论 #43722994 未加载
评论 #43723370 未加载
9d28 days ago
&gt; No guessing. No worrying about the order. Your code is self-documenting. Plus, TypeScript gives you full autocompletion and type safety.<p>1. Most of the time, arguments are not the same type, so if you&#x27;re using TypeScript, you&#x27;re already getting errors at this point. In your example, only first&#x2F;last names might get mixed up. And if you still get those wrong while writing that code, you&#x27;re just phoning it in atp and maybe should take a day off.<p>2. The same TypeScript that checks your types, also lets you see the signature when hovering over the function.<p>In real world coding, this isn&#x27;t an issue, and you&#x27;ll probably give up keeping this &quot;rule&quot; after a year or two. But good that you&#x27;re at least thinking about these kinds of things.
评论 #43720895 未加载
mx-bojangles28 days ago
I agree that the example is easier to read with the single object param. It might be even easier to read using a fluent interface or a builder pattern. You should choose the best tool for the job.<p>As a counter-example, I prefer<p><pre><code> setEnabled(item, true);</code></pre> to<p><pre><code> setEnabled({item: item, isEnabled: true});</code></pre>
评论 #43722662 未加载
tantalor28 days ago
&quot;Avoid the Long Parameter List&quot;<p><a href="https:&#x2F;&#x2F;testing.googleblog.com&#x2F;2024&#x2F;05&#x2F;avoid-long-parameter-list.html" rel="nofollow">https:&#x2F;&#x2F;testing.googleblog.com&#x2F;2024&#x2F;05&#x2F;avoid-long-parameter-...</a>
Joker_vD27 days ago
Here is a case were object parameters aren&#x27;t the better choice:<p><pre><code> type TBinarySearchParams&lt;T&gt; = { needle: T; haystack: [T]; }; const binarySearch = &lt;T,&gt;(args: TBinarySearchParams&lt;T&gt;) =&gt; { ... } binarySearch({needle: 42, haystack: [1,2,3]}); </code></pre> Or at least, <i>I</i> would argue they are not better, but if you still insist:<p><pre><code> type TIsEvenParams = { num: number; }; const isEven = (args: TIsEvenParams) =&gt; { ... } isEven({num: 7}); </code></pre> Surely <i>that</i> is strictly worse?
jiehong27 days ago
It’s known as the parameter object pattern[0].<p>A draw back would be is you want to overload such a method with fewer parameters, it then becomes weird.<p>[0]: <a href="https:&#x2F;&#x2F;wiki.ralfbarkow.ch&#x2F;view&#x2F;parameter-object" rel="nofollow">https:&#x2F;&#x2F;wiki.ralfbarkow.ch&#x2F;view&#x2F;parameter-object</a>
simianwords27 days ago
The other advantage to this pattern is that you can create a generic interface out of this.<p>With the N-params pattern it is not possible to create an interface on this method because the types of each param may be different across different implementation.
krystofee28 days ago
This is just because TS doesnt have keyword or keyword-only arguments as Python for example has.
joeyagreco28 days ago
In my opinion, this is the _only_ way to handle n-param functions in production code.