返回布尔值的 java 方法的命名约定(无问号)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3874350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 06:12:06  来源:igfitidea点击:

Naming conventions for java methods that return boolean(No question mark)

javamethodsnaming-conventions

提问by letronje

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, canin the front of a method sound okay for some cases. Is there a better way to name such methods?

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

For e.g. createFreshSnapshot?

例如createFreshSnapshot?

采纳答案by jjnguy

The convention is to ask a question in the name.

约定俗成就是以名字提问。

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

以下是一些可以在 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, truemeans yes, and falsemeans no.

然后,true意味着是,false意味着不是。

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.

注意:
有时您可能希望将方法命名为createFreshSnapshot?. 没有问号,这个名字意味着该方法应该创建一个快照,而不是检查是否需要一个快照。

In this case you should rethink what you are actually asking. Something like isSnapshotExpiredis 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.

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

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

如果你做一个谷歌搜索isEmpty()Java API中,你会得到很多结果。

回答by BillThor

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

标准是使用“is”或“has”作为前缀。例如isValidhasChildren

回答by Skilldrick

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

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

回答by Jason S

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:

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

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);

8.3.2 布尔属性

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

public boolean is<属性名称> ();

该“is< PropertyName>”方法可以代替“get< PropertyName>”方法提供,或者它可以作为“get< PropertyName>”方法的补充。在任何一种情况下,如果布尔属性存在“is< PropertyName>”方法,那么我们将使用“is< PropertyName>”方法来读取属性值。一个示例布尔属性可能是:

public boolean isMarsupial();
public void setMarsupial(boolean m);

回答by codymanix

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

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

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

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

对于所有其他情况,请使用前缀,如is..has..was..can..allows....

回答by kommradHomer

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

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

Java Programming Style Guidelines

Java 编程风格指南

Item 14 is specifically relevant and suggests the isprefix.

第 14 项特别相关并建议使用is前缀。

The style guide goes on to suggest:

风格指南继续建议:

There are a few alternatives to the isprefix that fits better in some situations. These are has, canand shouldprefixes:

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

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

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

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

shouldCreateFreshSnapshot()

回答by adrhc

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

我想对这个通用命名约定提出不同的观点,例如:

Set: boolean?add?(E?e)

设置boolean?add?(E?e)

where the rationale is:

理由是:

do some processing thenreportwhether succeeded or not.

做一些处理然后报告是否成功

While the returnis indeed a booleanthe method's name should point the processing to completeinstead of the result type(boolean for this example).

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

Your createFreshSnapshotexample 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 than the name createFreshSnapshotseems to be the best one for your situation.

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