返回布尔值的 Java 方法的命名约定

发布于 2024-09-26 08:14:17 字数 214 浏览 3 评论 0原文

我喜欢在其他语言中的方法/函数名称末尾使用问号。 Java 不允许我这样做。作为解决方法,我还能如何在 Java 中命名返回布尔值的方法?在某些情况下,在方法前面使用 ishasshouldcan 听起来不错。有没有更好的方法来命名这些方法?

例如createFreshSnapshot?

I like using question mark at the end of method/function names in other languages. Java doesn't let me do this. As a workaround how else can I name boolean returning methods in Java? Using an is, has, should, can in the front of a method sound okay for some cases. Is there a better way to name such methods?

For e.g. createFreshSnapshot?

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

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

发布评论

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

评论(7

宛菡 2024-10-03 08:14:17

惯例是以名义提出问题。

以下是 JDK 中可以找到的一些示例:

isEmpty()

hasChildren()

这样,名称读起来就像末尾带有问号一样。

集合是空的吗?
该节点有子节点吗?

然后,true 表示是,false 表示否。

或者,您可以将其视为断言:

集合为空。
该节点有子节点

注意:
有时您可能想将方法命名为 createFreshSnapshot? 之类的名称。如果没有问号,该名称意味着该方法应该创建快照,而不是检查是否需要快照。

在这种情况下,您应该重新考虑您实际要问的内容。像 isSnapshotExpired 这样的名称是一个更好的名称,它传达了该方法在调用时会告诉您的内容。遵循这样的模式还可以帮助您保持更多功能的纯净且没有副作用。

如果您执行在 Java API 中 Google 搜索 isEmpty(),您会得到很多结果。

The convention is to ask a question in the name.

Here are a few examples that can be found in the JDK:

isEmpty()

hasChildren()

That way, the names are read like they would have a question mark on the end.

Is the Collection empty?
Does this Node have children?

And, then, true means yes, and false means no.

Or, you could read it like an assertion:

The Collection is empty.
The node has children

Note:
Sometimes you may want to name a method something like createFreshSnapshot?. Without the question mark, the name implies that the method should be creating a snapshot, instead of checking to see if one is required.

In this case you should rethink what you are actually asking. Something like isSnapshotExpired is a much better name, and conveys what the method will tell you when it is called. Following a pattern like this can also help keep more of your functions pure and without side effects.

If you do a Google Search for isEmpty() in the Java API, you get lots of results.

乖乖 2024-10-03 08:14:17

如果您希望您的类与 Java Beans 规范,以便利用反射的工具(例如 JavaBuildersJGoodies Binding) 可以识别布尔值 getter,可以使用 getXXXX()isXXXX() 作为方法名称。来自 Java Bean 规范:

8.3.2 布尔属性

此外,对于布尔属性,我们允许 getter 方法来匹配模式:

public boolean is<PropertyName>();

这个“是<PropertyName>”可以提供方法来代替“get<PropertyName>”方法,或者除了“get<PropertyName>”之外还可以提供它方法。无论哪种情况,如果“is<PropertyName>”方法存在于布尔属性,那么我们将使用“is<PropertyName>”方法读取属性值。布尔属性示例可能是:

public boolean isMarsupial();
公共无效setMarsupial(布尔m);

If you wish your class to be compatible with the Java Beans specification, so that tools utilizing reflection (e.g. JavaBuilders, JGoodies Binding) can recognize boolean getters, either use getXXXX() or isXXXX() as a method name. From the Java Beans spec:

8.3.2 Boolean properties

In addition, for boolean properties, we allow a getter method to match the pattern:

public boolean is<PropertyName>();

This “is<PropertyName>” method may be provided instead of a “get<PropertyName>” method, or it may be provided in addition to a “get<PropertyName>” method. In either case, if the “is<PropertyName>” method is present for a boolean property then we will use the “is<PropertyName>” method to read the property value. An example boolean property might be:

public boolean isMarsupial();
public void setMarsupial(boolean m);
箜明 2024-10-03 08:14:17

我想发布此链接,因为它可能有助于进一步检查此答案并寻找更多 java 样式约定

Java编程风格指南

项目“2.13 is 前缀应用于布尔变量和方法。”特别相关并建议使用 is 前缀。

风格指南继续建议:

is 前缀有一些替代方案,更适合某些情况。这些是 hascanshould 前缀:

boolean hasLicense();
boolean canEvaluate();
boolean shouldAbort = false;

如果您遵循指南,我相信会命名适当的方法:

shouldCreateFreshSnapshot()

I want to post this link as it may help further for peeps checking this answer and looking for more java style convention

Java Programming Style Guidelines

Item "2.13 is prefix should be used for boolean variables and methods." is specifically relevant and suggests the is prefix.

The style guide goes on to suggest:

There are a few alternatives to the is prefix that fits better in some situations. These are has, can and should prefixes:

boolean hasLicense();
boolean canEvaluate();
boolean shouldAbort = false;

If you follow the Guidelines I believe the appropriate method would be named:

shouldCreateFreshSnapshot()
合约呢 2024-10-03 08:14:17

对于可能失败的方法,即指定布尔值作为返回类型,我将使用前缀 try

if (tryCreateFreshSnapshot())
{
  // ...
}

对于所有其他情况,请使用诸如 is.. has 之类的前缀.. 曾经.. 可以.. 允许.. ..

For methods which may fail, that is you specify boolean as return type, I would use the prefix try:

if (tryCreateFreshSnapshot())
{
  // ...
}

For all other cases use prefixes like is.. has.. was.. can.. allows.. ..

情深缘浅 2024-10-03 08:14:17

标准是使用 ishas 作为前缀。例如isValidhasChildren

Standard is use is or has as a prefix. For example isValid, hasChildren.

反差帅 2024-10-03 08:14:17

is 是我遇到最多的一个。但在目前的情况下,任何有意义的选择都是最好的选择。

is is the one I've come across more than any other. Whatever makes sense in the current situation is the best option though.

诠释孤独 2024-10-03 08:14:17

我想对这个一般命名约定提出不同的看法,例如:

参见java.util.Set: boolean add (E e)

其中基本原理是:

进行一些处理,然后 报告 是否成功

虽然return确实是一个boolean,但方法的名称应该指向要完成的处理而不是结果类型(本例中的布尔值)。

对我来说,您的 createFreshSnapshot 示例似乎与此观点更相关,因为似乎意味着:创建一个新快照,然后报告创建操作是否成功。考虑到这一推理,名称 createFreshSnapshot 似乎是最适合您的情况的名称。

I want to point a different view on this general naming convention, e.g.:

see java.util.Set: boolean add​(E e)

where the rationale is:

do some processing then report whether it succeeded or not.

While the return is indeed a boolean the method's name should point the processing to complete instead of the result type (boolean for this example).

Your createFreshSnapshot example seems for me more related to this point of view because seems to mean this: create a fresh-snapshot then report whether the create-operation succeeded. Considering this reasoning the name createFreshSnapshot seems to be the best one for your situation.

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