TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

N-Params vs. Single Param

37 点作者 carlos-menezes26 天前

11 条评论

disillusionist26 天前
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 未加载
hwpythonner26 天前
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 未加载
sixo26 天前
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 未加载
9d26 天前
&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-bojangles26 天前
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 未加载
tantalor26 天前
&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_vD25 天前
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?
jiehong25 天前
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>
simianwords25 天前
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.
krystofee26 天前
This is just because TS doesnt have keyword or keyword-only arguments as Python for example has.
joeyagreco26 天前
In my opinion, this is the _only_ way to handle n-param functions in production code.