Java:如何命名布尔属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2945061/
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
Java: how to name boolean properties
提问by NoozNooz42
I just had a little surprise in a Webapp, where I'm using EL in .jsp pages.
我只是在一个 Webapp 中有点意外,我在 .jsp 页面中使用了 EL。
I added a boolean property and scratched my head because I had named a boolean "isDynamic", so I could write this:
我添加了一个布尔属性并挠头,因为我已经命名了一个布尔值“isDynamic”,所以我可以这样写:
<c:if test="${page.isDynamic}">
...
</c:if>
Which I find easier to read than:
我发现它比以下内容更容易阅读:
<c:if test="${page.dynamic}">
...
</c:if>
However the .jsp failed to compile, with the error:
但是 .jsp 无法编译,错误如下:
javax.el.PropertyNotFoundException: Property 'isDynamic' not found on type com...
I turns out my IDE (and it took me some time to notice it), when generating the getter, had generated a method called:
我发现我的 IDE(我花了一些时间才注意到它)在生成 getter 时生成了一个名为:
isDynamic()
instead of:
代替:
getIsDynamic()
Once I manually replaced isDynamic()by getIsDynamic()everything was working fine.
有一次,我手动更换isDynamic()由getIsDynamic()一切工作正常。
So I've got really two questions here:
所以我真的有两个问题:
is it bad to start a boolean property's name with "is"?
wether it is bad or not, didn't IntelliJ made a mistake here by auto-generating a method named isDynamicinstead of getIsDynamic?
以“is”开头布尔属性的名称是否不好?
无论它是否糟糕,IntelliJ 是否通过自动生成名为isDynamic而不是getIsDynamic的方法在这里犯了一个错误?
采纳答案by BalusC
Sensitive subject, but in my opinionit is bad. The variable name should not denote a question, but a statement. E.g.
pageIsDynamic
,dynamical
ordynamicallyGenerated
. There is however no clear coding convention for this. As long as you're consistent throughout the coding, either way won't harm that much.No, it didn't. The Javabean specificationstates that it is allowed to prefix boolean getter method names with
is
as well. It is usually preferred aboveget
. As every other decent IDE, IntellIJ just adheres this specification. Eclipse and Netbeans would do the same. Here's an extract of chapter 8.3.2:
敏感话题,但在我看来它很糟糕。变量名不应该表示一个问题,而是一个陈述。例如
pageIsDynamic
,dynamical
或dynamicallyGenerated
。然而,对此没有明确的编码约定。只要您在整个编码过程中保持一致,无论哪种方式都不会造成太大伤害。不,它没有。该JavaBean规范规定,允许其前缀布尔getter方法的名称与
is
为好。通常首选以上get
。与其他所有不错的 IDE 一样,IntellIJ 只是遵守此规范。Eclipse 和 Netbeans 也会这样做。这是第 8.3.2 章的摘录:
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<PropertyName>();
“is<PropertyName>”
可以提供该方法来代替“get<PropertyName>”
方法,或者除了提供“get<PropertyName>”
方法之外还可以提供该方法。在任何一种情况下,如果该
“is<PropertyName>”
方法适用于布尔属性,那么我们将使用该“is<PropertyName>”
方法读取属性值。一个示例布尔属性可能是:public boolean isMarsupial(); public void setMarsupial(boolean m);
回答by Greg Charles
It's more typical to name the property without "is" and let the accessor have "is". You can certainly change what your IDE generates though, and have "getIsDynamic()" be the accessor if that's clearer for you.
更典型的做法是将属性命名为不带“is”而让访问器具有“is”。不过,您当然可以更改 IDE 生成的内容,如果您更清楚,可以将“getIsDynamic()”作为访问器。
回答by Hyman
Since in Java you don't have clash between variable names and method it would say that it's ok to have an isDynamic()
method that returns if isDynamic
is true
. Or at least this is good if the "dinamicity" is actually a real attribute of the object and not just a boolean value that you need.
因为在 Java 中你没有变量名和方法之间的冲突,所以它会说可以有一个isDynamic()
返回 if isDynamic
is的方法true
。或者,如果“动态性”实际上是对象的真实属性,而不仅仅是您需要的布尔值,那么至少这很好。
For example verbose
is a boolean value that is usually not an attribute of an object, so having a isVerbose()
method would be a bad idea (unless it's a Console
class).
例如verbose
,一个布尔值通常不是对象的属性,因此拥有一个isVerbose()
方法将是一个坏主意(除非它是一个Console
类)。
Having a boolean called isDynamic
is a good expressive idea. It suggests you that the variable is a bool
without any additional effort.
有一个布尔值isDynamic
是一个很好的表达想法。它建议您变量是 abool
无需任何额外的努力。
回答by André van Toly
isDynamic() is normally the way to go as a boolean getter.
isDynamic() 通常是作为布尔 getter 的方法。
public boolean isDynamic() {
return dynamic;
}
in your template you can use:
在您的模板中,您可以使用:
<c:if test="${dynamic}">
...
</c:if>