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