Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: how to name boolean properties

Tags:

java

jsp

el

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:

  1. is it bad to start a boolean property's name with "is"?

  2. wether it is bad or not, didn't IntelliJ made a mistake here by auto-generating a method named isDynamic instead of getIsDynamic?

like image 688
NoozNooz42 Avatar asked May 31 '10 17:05

NoozNooz42


People also ask

How do you name boolean properties?

✔️ DO name Boolean properties with an affirmative phrase ( CanSeek instead of CantSeek ). Optionally, you can also prefix Boolean properties with "Is", "Can", or "Has", but only where it adds value. ✔️ CONSIDER giving a property the same name as its type.

How do you name boolean values?

When naming booleans, you should avoid choosing variable names that include negations. It's better to name the variable without the negation and flip the value. If you absolutely can't (see Guideline 2 below), then try to find an already-negated form of the concept you are trying to express.

How do you define a boolean in Java?

In Java, the boolean keyword is a primitive data type. It is used to store only two possible values, either true or false. It specifies 1-bit of information and its "size" can't be defined precisely.

Does boolean need to be capitalized in Java?

The Boolean data type is capitalized when we talk about it. This is because it was named after the mathematician George Boole, who played a key role in the fields of logic and algebra. However, when you are declaring a boolean, you should use lowercase. In Java, booleans are declared using the boolean keyword.


3 Answers

  1. Sensitive subject, but in my opinion it is bad. The variable name should not denote a question, but a statement. E.g. pageIsDynamic, dynamical or dynamicallyGenerated. 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.

  2. 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 above get. 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);
like image 100
BalusC Avatar answered Oct 17 '22 21:10

BalusC


isDynamic() is normally the way to go as a boolean getter.

public boolean isDynamic() {
  return dynamic;
}

in your template you can use:

<c:if test="${dynamic}">
 ...
</c:if>
like image 44
André van Toly Avatar answered Oct 17 '22 21:10

André van Toly


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.

like image 1
Greg Charles Avatar answered Oct 17 '22 21:10

Greg Charles