如何命名具有 out 参数的方法?

发布于 2024-08-11 06:28:54 字数 150 浏览 8 评论 0原文

命名内部具有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

弄潮 2024-08-18 06:28:54

命名没有标准。但是,如果您的方法要作用于复合类型,请考虑采用使用 Get...And...() 的约定来指示正在发生两件事。例如:

int GetPopulationAndMeanAge(out double meanAge)
{
    // ...
    meanAge = CalculateMeanAge();
    return totalPopulation;
}

我认为更好的方法是返回复合类型。在垃圾收集语言中,确实没有理由这样做,除非这样的方法被调用了数百万次,并且检测显示 GC 没有正确处理该方法。加载。在非 GC 语言中,它提出了一个小问题,即确保清楚谁负责在完成后清理内存。

将前面的重构为复合类型(C#):

public class PopulationStatistics {
    int Population { get; set; }
    double MeanAge { get; set; }
}


PopulationStatistics GetPopulationStatistics()
{
    // ...
    return new PopulationStatistics { Population = totalPopulation, MeanAge = CalculateMeanAge };
}

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:

int GetPopulationAndMeanAge(out double meanAge)
{
    // ...
    meanAge = CalculateMeanAge();
    return totalPopulation;
}

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#):

public class PopulationStatistics {
    int Population { get; set; }
    double MeanAge { get; set; }
}


PopulationStatistics GetPopulationStatistics()
{
    // ...
    return new PopulationStatistics { Population = totalPopulation, MeanAge = CalculateMeanAge };
}
命硬 2024-08-18 06:28:54

方法名称应该描述该方法的功能(自文档代码)。如果方法签名表明它使用输出参数,则签名应足以提醒开发人员变量中将返回一个值。因此,我认为将其包含在方法名称中是多余的。即使是自记录代码也应该清晰简洁。如果您的语言没有明确说明这一点,那么我要么在名称中记录它(如果可以清晰简洁地完成),要么使用内联注释。

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.

策马西风 2024-08-18 06:28:54

为什么不返回该参数呢?

无论如何,您可以使用“修改”或“处理”前缀。

Why not return that parameter instead?

Anyway, you could use "modify" or "handle" prefix.

望她远 2024-08-18 06:28:54

我想说,在这种情况下,保持命名一致比命名方案实际是什么更重要。对于那些在你后面编写代码的人来说,这让事情变得更容易,因为他们会根据方法的命名方式知道对你的方法的期望。

话虽如此,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.

单调的奢华 2024-08-18 06:28:54

这取决于语言。
例如,在 C# 中,无需将其添加到函数名称中,它已经在签名中:如果不再次指定 out 则无法调用该函数,因此不存在遗漏侧面的风险影响:

Int32.TryParse("123", out number);

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:

Int32.TryParse("123", out number);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文