如何命名具有 out 参数的方法?
命名内部具有 out 参数的方法的常见偏好是什么?
通常我使用 Get 作为前缀来提及该方法返回一个值(如 GetMyBusiness)。
但是,如果方法调用后要设置一个输出参数怎么办? 方法名称是否应该提及这一点并仅关注返回值?
谢谢!
What is the common preference to name a method that has an out parameter inside?
Usually I use Get as a prefix to mention that the method returns a value (like GetMyBusiness).
But what if there is an out parameter that will be set after the method call?
Should the method name mention this and focus only the return value?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
命名没有标准。但是,如果您的方法要作用于复合类型,请考虑采用使用 Get...And...() 的约定来指示正在发生两件事。例如:
我认为更好的方法是返回复合类型。在垃圾收集语言中,确实没有理由不这样做,除非这样的方法被调用了数百万次,并且检测显示 GC 没有正确处理该方法。加载。在非 GC 语言中,它提出了一个小问题,即确保清楚谁负责在完成后清理内存。
将前面的重构为复合类型(C#):
There is no standard in nomenclature. However, if your method is going to be acting on compound types, consider adopting a convention of using Get...And...() to indicate that there are two things going on. For example:
I think the better approach is to return a compound type instead. In a garbage collected language, there is really no excuse NOT to do this, except in cases where such a method is called, say, millions of times and instrumentation reveals that the GC isn't properly handling the load. In non-GC languages, it presents a minor issue in terms of making sure that it's clear who is responsible for cleaning up the memory when you're done.
Refactoring the previous into a compound type (C#):
方法名称应该描述该方法的功能(自文档代码)。如果方法签名表明它使用输出参数,则签名应足以提醒开发人员变量中将返回一个值。因此,我认为将其包含在方法名称中是多余的。即使是自记录代码也应该清晰简洁。如果您的语言没有明确说明这一点,那么我要么在名称中记录它(如果可以清晰简洁地完成),要么使用内联注释。
The method name should describe the function of the method (self-documenting code). If the method signature will indicate that it uses an out parameter, the signature should be sufficient to alert developers that a value will be returned in the variable. I would consider it to be redundant, therefore, to include this in the method name. Even self-documenting code should be clear and concise. If your language doesn't make this clear then I would either document it in the name, if it can be done clearly and concisely, or using inline comments.
为什么不返回该参数呢?
无论如何,您可以使用“修改”或“处理”前缀。
Why not return that parameter instead?
Anyway, you could use "modify" or "handle" prefix.
我想说,在这种情况下,保持命名一致比命名方案实际是什么更重要。对于那些在你后面编写代码的人来说,这让事情变得更容易,因为他们会根据方法的命名方式知道对你的方法的期望。
话虽如此,
Get
应该就可以了。I'd say in this case it's more important to keep your naming consistent rather than what your naming scheme actually is. It makes things easier for those who code behind you, since they will know what to expect from your methods based on how they're named.
Having said that,
Get
should be just fine.这取决于语言。
例如,在 C# 中,无需将其添加到函数名称中,它已经在签名中:如果不再次指定
out
则无法调用该函数,因此不存在遗漏侧面的风险影响:That depends on the language.
In C#, for example, there's no need to add it to the name of the function, it's already in the signature: you cannot call the function without specifying
out
again, so there's no risk of missing the side effect: