IsRunning() or GetIsRunning()?

H

howa

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

Lew

howa said:
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....

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
 
O

Oliver Wong

howa said:
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....

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
 
K

Knute Johnson

Lew said:
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

Most methods that determine state are isSomething. I usually use a
variable called runFlag and isRunning(). But as Lew said, season to taste.
 
A

Andreas Leitgeb

howa said:
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.
 
T

Thomas Fritsch

Lew said:
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.
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...Descriptor(java.lang.String, java.lang.Class)>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top