I have a class, which has a variable store the state of the class,
e.g. isRunning
However, when I want to add a public method to access to this
variable, I wonder should I use getIsRunning or simply IsRunning?
seems getter (getXXX) is a normal way, but the method name seem to be
dummy....
Lew - 21 Feb 2007 15:50 GMT
> I have a class, which has a variable store the state of the class,
> e.g. isRunning
[quoted text clipped - 4 lines]
> seems getter (getXXX) is a normal way, but the method name seem to be
> dummy....
This is a matter of taste, but it is popular for an instance boolean variable,
say
private boolean running;
not to have any prefix (same as other instance variables), for the setter to
be named set...() and the getter is...(), where the ... matches the variable
name in the usual way:
public void setRunning( boolean running );
public boolean isRunning();
The idiom "getRunning()" is also used, but less commonly.
I do not know if the JavaBean specification has rules for this.
- Lew
Knute Johnson - 21 Feb 2007 16:17 GMT
>> I have a class, which has a variable store the state of the class,
>> e.g. isRunning
[quoted text clipped - 22 lines]
>
> - Lew
Most methods that determine state are isSomething. I usually use a
variable called runFlag and isRunning(). But as Lew said, season to taste.

Signature
Knute Johnson
email s/nospam/knute/
howa - 21 Feb 2007 16:17 GMT
> I do not know if the JavaBean specification has rules for this.
>
> - Lew
Yes, this perhaps is the main concern...
Thomas Fritsch - 21 Feb 2007 16:50 GMT
> This is a matter of taste, but it is popular for an instance boolean
> variable, say
[quoted text clipped - 11 lines]
>
> I do not know if the JavaBean specification has rules for this.
Actually the JavaBean spec has such a rule. It is in the API doc of
constructor PropertyDescriptor(String,Class))
<http://java.sun.com/j2se/1.4.2/docs/api/java/beans/PropertyDescriptor.html#Prope
rtyDescriptor(java.lang.String,%20java.lang.Class)>

Signature
Thomas
Oliver Wong - 21 Feb 2007 16:13 GMT
>I have a class, which has a variable store the state of the class,
> e.g. isRunning
[quoted text clipped - 4 lines]
> seems getter (getXXX) is a normal way, but the method name seem to be
> dummy....
Most tools I've worked with expect "get" as a prefix for non-boolean
fields, and "is" for boolean fields. If you want to be able to interoperate
with those tools, use "isRunning()" (lowercase 'i') rather than
"getIsRunning()".
- Oliver
Andreas Leitgeb - 21 Feb 2007 16:28 GMT
> I have a class, which has a variable store the state of the class,
> e.g. isRunning
> However, when I want to add a public method to access to this
> variable, I wonder should I use getIsRunning or simply IsRunning?
> seems getter (getXXX) is a normal way, but the method name seem to be
> dummy....
First, for all style conventions' sake, you definitely should
NOT call it "IsRunning" (with capital first letter).
Mixed-case-starting-w/-capital is reserved for Class-names.
Second, I wouldn't call the field "isRunning", but perhaps
just "running". I'm not sure if this is covered by Style-
guides, though.
For "isRunning()" versus "getRunning()" (or "getIsRunning()"),
I'd decide to use get...(), if the method really just returns
the attribute, and is...() if the information returned is more
or less calculated, like in "return state==State::RUNNING".
But this rule is not very strong, so isRunning() is still ok,
even if you just return the field's value.