Java:如何命名布尔属性 [英] Java: how to name boolean properties
问题描述
我只是在一个Web应用程序中有一点惊喜,我在.jsp页面中使用EL。
我添加了一个布尔属性并抓住了我的头,因为我我已经命名了一个布尔isDynamic,所以我可以这样写:
< c:if test =$ {page。 isDynamic}>
...
< / c:if>
我觉得比以下更容易阅读:
< c:if test =$ {page.dynamic}>
...
< / c:if>
然而.jsp无法编译,错误如下:
javax.el.PropertyNotFoundException:在类型com上找不到属性'isDynamic'...
我发现我的IDE(我花了一些时间注意它),在生成getter时,生成了一个名为的方法:
isDynamic()
而不是:
getIsDynamic()
一旦我通过 getIsDynamic()手动更换 isDynamic(),一切正常。
所以我在这里有两个问题:
-
使用是启动布尔属性的名称是不是很糟糕? / p>
-
这是不是坏了,没有IntelliJ在这里通过自动生成一个名为 isDynamic 的方法而不是 getIsDynamic ?
-
敏感主题,但我认为这很糟糕。变量名称不应表示问题,而应表示语句。例如。
pageIsDynamic
,dynamicical
或dynamicGenerated
。然而,对此没有明确的编码约定。只要你在整个编码过程中保持一致,无论哪种方式都不会伤害那么多。 -
不,它没有。 Javabean规范规定允许前缀为boolean带
的getter方法名称也是
。通常首选get
。与其他所有体面的IDE一样,IntellIJ只是坚持这个规范。 Eclipse和Netbeans也会这样做。以下是第8.3.2章的摘录:
8.3.2布尔属性
此外,对于布尔属性,我们允许getter方法匹配模式:
public boolean is< PropertyName>();
此
是< PropertyName>
方法可以提供而不是get< PropertyName>
方法,
或者除了get< PropertyName> ;
方法。
在任何一种情况下,如果
是< PropertyName>
对于布尔属性存在>方法,然后我们将使用is< PropertyName>
方法来读取属性值。
示例布尔属性可能是:public boolean isMarsupial();
public void setMarsupial(boolean m);
I just had a little surprise in a Webapp, where I'm using EL in .jsp pages.
I added a boolean property and scratched my head because I had named a boolean "isDynamic", so I could write this:
<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:
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:
isDynamic()
instead of:
getIsDynamic()
Once I manually replaced isDynamic() by getIsDynamic() everything was working fine.
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 isDynamic instead of getIsDynamic?
Sensitive subject, but in my opinion it 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 specification states 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:
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);
这篇关于Java:如何命名布尔属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!