简体   繁体   English

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

[英]Naming conventions for Java methods that return boolean

I like using question mark at the end of method/function names in other languages.我喜欢在其他语言的方法/函数名称末尾使用问号。 Java doesn't let me do this. Java 不允许我这样做。 As a workaround how else can I name boolean returning methods in Java?作为一种解决方法,我还能如何在 Java 中命名布尔返回方法? Using an is , has , should , can in the front of a method sound okay for some cases.使用的ishasshouldcan在好一些情况下,方法声音的前端。 Is there a better way to name such methods?有没有更好的方法来命名这些方法?

For eg createFreshSnapshot?例如createFreshSnapshot?

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, true means yes, and false means 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?有时您可能想将方法命名为类似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.isSnapshotExpired这样的isSnapshotExpired是一个更好的名称,它传达了该方法在被调用时会告诉您的信息。 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.如果您在 Java API 中对isEmpty()进行Google 搜索,您会得到很多结果。

If you wish your class to be compatible with the Java Beans specification , so that tools utilizing reflection (eg JavaBuilders , JGoodies Binding ) can recognize boolean getters, either use getXXXX() or isXXXX() as a method name.如果您希望您的类与Java Beans 规范兼容,以便利用反射的工具(例如JavaBuildersJGoodies Binding )可以识别布尔 getter,请使用getXXXX()isXXXX()作为方法名称。 From the Java Beans spec:来自 Java Beans 规范:

8.3.2 Boolean properties 8.3.2 布尔属性

In addition, for boolean properties, we allow a getter method to match the pattern:此外,对于布尔属性,我们允许使用 getter 方法来匹配模式:

public boolean is < PropertyName > () ; 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.该“is< PropertyName >”方法可以代替“get< PropertyName >”方法提供,或者它可以作为“get< PropertyName >”方法的补充。 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.在任何一种情况下,如果布尔属性存在“is< PropertyName >”方法,那么我们将使用“is< PropertyName >”方法来读取属性值。 An example boolean property might be:一个示例布尔属性可能是:

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

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 "2.13 is prefix should be used for boolean variables and methods."条目“2.13前缀应该用于布尔变量和方法。” is specifically relevant and suggests the is prefix.是特别相关的,并建议使用is前缀。

The style guide goes on to suggest:风格指南继续建议:

There are a few alternatives to the is prefix that fits better in some situations.在某些情况下,有一些更适合is前缀的替代方案。 These are has , can and should prefixes:这些是hascanshould前缀:

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

If you follow the Guidelines I believe the appropriate method would be named:如果您遵循指南,我相信适当的方法将被命名为:

shouldCreateFreshSnapshot()

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.. ..

Standard is use is or has as a prefix.标准是使用ishas作为前缀。 For example isValid , hasChildren .例如isValidhasChildren

is is the one I've come across more than any other. is我遇到的最多的一个。 Whatever makes sense in the current situation is the best option though.不过,在当前情况下,任何有意义的事情都是最好的选择。

I want to point a different view on this general naming convention, eg:我想对这个通用命名约定提出不同的观点,例如:

see java.util.Set : boolean add​(E e)参见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).虽然return确实是一个boolean但方法的名称应该指向要完成的处理而不是结果类型(本例中为布尔值)。

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.您的createFreshSnapshot示例对我来说似乎与这个观点更相关,因为似乎意味着:创建一个新快照然后报告创建操作是否成功。 Considering this reasoning the name createFreshSnapshot seems to be the best one for your situation.考虑到这一推理,名称createFreshSnapshot似乎是最适合您的情况的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM