返回布尔值的 Java 方法的命名约定
我喜欢在其他语言中的方法/函数名称末尾使用问号。 Java 不允许我这样做。作为解决方法,我还能如何在 Java 中命名返回布尔值的方法?在某些情况下,在方法前面使用 is
、has
、should
、can
听起来不错。有没有更好的方法来命名这些方法?
例如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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
惯例是以名义提出问题。
以下是 JDK 中可以找到的一些示例:
这样,名称读起来就像末尾带有问号一样。
然后,
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:
That way, the names are read like they would have a question mark on the end.
And, then,
true
means yes, andfalse
means no.Or, you could read it like an assertion:
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.如果您希望您的类与 Java Beans 规范,以便利用反射的工具(例如 JavaBuilders、JGoodies Binding) 可以识别布尔值 getter,可以使用
getXXXX()
或isXXXX()
作为方法名称。来自 Java Bean 规范: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()
orisXXXX()
as a method name. From the Java Beans spec:我想发布此链接,因为它可能有助于进一步检查此答案并寻找更多 java 样式约定
Java编程风格指南
项目“2.13 is 前缀应用于布尔变量和方法。”特别相关并建议使用 is 前缀。
风格指南继续建议:
如果您遵循指南,我相信会命名适当的方法:
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:
If you follow the Guidelines I believe the appropriate method would be named:
对于可能失败的方法,即指定布尔值作为返回类型,我将使用前缀
try
:对于所有其他情况,请使用诸如
is..
has 之类的前缀..
曾经..
可以..
允许..
..For methods which may fail, that is you specify boolean as return type, I would use the prefix
try
:For all other cases use prefixes like
is..
has..
was..
can..
allows..
..标准是使用
is
或has
作为前缀。例如isValid
、hasChildren
。Standard is use
is
orhas
as a prefix. For exampleisValid
,hasChildren
.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.我想对这个一般命名约定提出不同的看法,例如:
参见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 aboolean
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 namecreateFreshSnapshot
seems to be the best one for your situation.