(评论)
(comments)

原始链接: https://news.ycombinator.com/item?id=43720457

这篇 Hacker News 讨论帖讨论了在函数定义中使用单个参数对象代替多个独立参数的做法,尤其是在 JavaScript 中。 原作者 carlos-menezes 倡导这种方法,因为它可以提高代码可读性、变量命名的一致性以及代码的自文档化程度。评论者们就其优缺点展开了辩论。一些人表示赞同,强调了这种方法在扩展性和 TypeScript 类型安全方面的优势。一位用户提到返回值缺乏固有的名称,使得函数目的不够清晰。另一些人认为,在 C/C++ 等语言中,这种模式可能会由于结构体与寄存器相比可能造成的开销而对性能产生负面影响。另一位用户建议编译器理想情况下可以优化掉这种低效。许多人认为这是一种个人偏好,取决于具体的上下文和项目复杂性,并链接了 Google 的指导方针:“避免过长的参数列表”。

相关文章
  • N参数 vs. 单参数 2025-04-17
  • (评论) 2023-11-28
  • (评论) 2025-03-13
  • 别害怕类型 2025-03-22
  • (评论) 2024-04-20

  • 原文
    Hacker News new | past | comments | ask | show | jobs | submit login
    N-Params vs. Single Param (carlos-menezes.com)
    9 points by carlos-menezes 1 hour ago | hide | past | favorite | 8 comments










    I started using this pattern years ago and haven'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.

    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.

    ie I will usually write something like `const firstName = getFirstName(); doTheThing({ firstName });` rather than `const fName = getFirstName(); doTheThing({ firstName: fName });`



    What bothers me a lot is that return values can't have a name. Suppose I have a function

    string combineName(string first, string last);

    Okay, I assume the result is the full name, but I don't really know that like I would if the return had a name in the header. (The function can be getFullName, yes, but that's so simple it doesn't matter).



    Likewise. Even if the input object only has a single property, I'll still use this pattern.


    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.

    But in native languages like C/C++/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/padding issues. Also, passing a const struct doesn’t guarantee the fields inside are truly const.

    That said, it's context dependent. If you'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.



    I think the dream has long been that a compiler could optimize away any inefficiencies that can be mechanically described. As such, if there is a benefit to seeing the same structure in many places, using a struct would be ideal.

    I don't think we are near said dream, yet.



    > No guessing. No worrying about the order. Your code is self-documenting. Plus, TypeScript gives you full autocompletion and type safety.

    1. Most of the time, arguments are not the same type, so if you're using TypeScript, you're already getting errors at this point. In your example, only first/last names might get mixed up. And if you still get those wrong while writing that code, you're just phoning it in atp and maybe should take a day off.

    2. The same TypeScript that checks your types, also lets you see the signature when hovering over the function.

    In real world coding, this isn't an issue, and you'll probably give up keeping this "rule" after a year or two. But good that you're at least thinking about these kinds of things.



    It’s a preference grounded in long-term ergonomics that work well for me. I’ve been using this "rule" for over two years now and haven’t found a reason to give it up.








    Join us for AI Startup School this June 16-17 in San Francisco!


    Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact



    Search:
    联系我们 contact @ memedata.com