可以修改(写回)参数的函数和方法的良好命名约定

发布于 2024-08-21 04:11:00 字数 972 浏览 5 评论 0原文

我需要为处理“值数组”的例程找到一个好的且易于理解的命名方案(我已经在 J​​ava 中编写了类似于 C++ 的 valarray 的东西,并对原始类型进行了一些优化介意)。

我知道例程的主要分类是在以下之间进行的:

  • 函数(它们可能接受参数并且必须返回某些内容)
  • 方法(它们可能接受参数并且不返回任何内容)

出于性能原因,我允许不仅定义以下函数/方法威胁其参数为只读,但也可能修改其第一个参数的函数/方法。

这样人们不仅可以这样做...:

ValArrayInt a=..., b=...;
// "apply" treats "a" and "b" as readonly
ValArrayInt temp = ValArrays.apply(adder, a, b); // temp = a + b
a = temp;

...还可以这样做:

ValArrayInt a=..., b=...;
// "apply" modifies "a"
a.apply(adder, b); // a += b

请为这些例程建议一种命名方案: strong>

  • 函数,将所有参数视为只读
  • 函数可以修改其第一个参数
  • 方法只读
  • 方法可以修改它们的第一个参数

我想到了像ModifyingMethod、NonModifyingMethod或类似的东西,但是我认为这些名字不够简单,而且太长。

I need to find a good and understandable naming-scheme for routines which deal with "value arrays" (I've written something similar to C++'s valarray in Java, with some optimizations for primitive types in mind).

I know that the main categorization of routines is made between:

  • functions (they may take parameters and must return something)
  • methods (they may take parameters and don't return anything)

For performance reasons, I allow to define not only functions/methods which threat their parameters as readonly, but also functions/methods which may modify their first parameter.

So that one can not only do this... :

ValArrayInt a=..., b=...;
// "apply" treats "a" and "b" as readonly
ValArrayInt temp = ValArrays.apply(adder, a, b); // temp = a + b
a = temp;

... but also this:

ValArrayInt a=..., b=...;
// "apply" modifies "a"
a.apply(adder, b); // a += b

Please, suggest a naming scheme for these kinds of routines:

  • functions which treat all parameters as readonly
  • functions which may modify their first parameter
  • methods which treat all parameters as readonly
  • methods which may modify their first parameter

I have thought of something like ModifyingMethod, NonModifyingMethod, or similar, but I think those names are not straightforward enough, and too long.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

紧拥背影 2024-08-28 04:11:00

我总是在方法名称中使用“更新”一词来指示一个或多个参数将因此而更新。例如:

updateClaimWithPolicyDetails(Claim c, PolicyCriteria pc, String identifier)

您可以合理地推断出此方法可能更新Claim对象。然后,如果用户想了解更多信息,他可以深入研究 javadoc

对于返回某些内容并更新参数的函数,可以使用“updateAndGet”之类的函数。

public Policy updateClaimAndGetPolicy(Claim c, PolicyCriteria pc, String identifier) 

由此您可以推断出人们正在更新索赔并返回保单(索赔可能已经针对保单进行了更新)。

如果方法中未使用 updateXXX 一词,那么您应该假设所有参数都是只读的。如果您有两个可更新参数,那么您可以说

updateClaimAndPolicyCriteria(Claim C, PolicyCriteria pc, String junkParam)

上面可以告诉您索赔和保单标准都将更新。

I always use the word "update" in a method name to indicate that one or more of the parameters will be updated as a result. For ex:

updateClaimWithPolicyDetails(Claim c, PolicyCriteria pc, String identifier)

You can reasonably deduce that this method might update the Claim object. Then if a user wants to know more he can dig into the javadoc

For functions that return something and also update parameters something like "updateAndGet" could be used.

public Policy updateClaimAndGetPolicy(Claim c, PolicyCriteria pc, String identifier) 

From this you can deduce that one is updating the Claim and returning a policy (the claim may be already updated for the policy).

If the word updateXXX is not used in a method then you should assume that all params are readonly. If you have two updatable parameters then you could say

updateClaimAndPolicyCriteria(Claim C, PolicyCriteria pc, String junkParam)

The above can tell you that both claim and policy criteria will be updated.

心如荒岛 2024-08-28 04:11:00

将此信息编码到方法名称中可能很复杂。

另一方面,通过明确说明该方法的副作用,该信息必须存在于该方法的文档中(例如,在Javadoc 上)。

It may be complex to encode this info into the method name.

On the other hand this info must exist on the documentation of the method (e.g on the Javadoc) by explicitly stating the sideeffects of the method.

空心空情空意 2024-08-28 04:11:00

我喜欢上面的预挂更新或修改的答案。然而,这实际上应该是一个函数文档,用于描述调用函数的作用以及它如何影响参数。但是,通常您不想修改传递给函数的参数,因为在某些情况下(例如 Collections.sort)您希望这样做以防止复制。另外,请确保在适当的情况下使用不可变对象,而不是总是使用一些愚蠢的 Java bean 类以及所有公共 getter 和 setter。

I like the above answer of pre-pending update or modify. However, this should really be a function of documentation to describe what calling a function does and how it affects the parameters. However, usually you don't want to modify parameters passed to a function, thought there are cases (like Collections.sort) where you want to do this to prevent a copy. Also, make sure to use immutable objects where appropriate instead of always using some stupid java bean class with all public getters and setters.

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