tags:

views:

114

answers:

4

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?

+1  A: 

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.

Greg Charles
@Greg Charles: that's interesting... So when accessing such a property from EL (for example), you're losing the 'benefit' of the "isXXX" (seen that in EL you type the property's name directly, not the getter method name). I mean, when you read *"isXXX"* you know the answer is either yes/no (true/false). In the case where you name the property without the "is", you hence lose that info altogether.
NoozNooz42
@Greg Charles: that doesn't work. I just tried: if you name the bean property without the *"is"* and name your getter method *"isDynamic"* then EL cannot find the bean.
NoozNooz42
@Greg Charles: actually it worked, BalusC reminded me to change my EL and now it works. Thanks all :)
NoozNooz42
+4  A: 
  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);
BalusC
@BalusC: +1 thanks :)
NoozNooz42
The getter should be e.g. `isDynamical()` and EL should be written as `${page.dynamical}`.
BalusC
@BalusC: yup that was it, thanks a lot :)
NoozNooz42
You're welcome.
BalusC
+1  A: 

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.

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

Having a boolean called isDynamic is a good expressive idea. It suggests you that the variable is a bool without any additional effort.

Jack
+2  A: 

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>
André van Toly