Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > IsRunning() or GetIsRunning()?

Reply
Thread Tools

IsRunning() or GetIsRunning()?

 
 
howa
Guest
Posts: n/a
 
      02-21-2007
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....

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      02-21-2007
howa wrote:
> 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
 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      02-21-2007

"howa" <> wrote in message
news: oups.com...
>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


 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      02-21-2007
Lew wrote:
> howa wrote:
>> 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


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

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
howa
Guest
Posts: n/a
 
      02-21-2007


> I do not know if the JavaBean specification has rules for this.
>
> - Lew


Yes, this perhaps is the main concern...

 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      02-21-2007
howa <> wrote:
> 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.

 
Reply With Quote
 
Thomas Fritsch
Guest
Posts: n/a
 
      02-21-2007
Lew wrote:
> 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/api/java/beans/PropertyDescriptor.html#PropertyDescriptor(java.la ng.String,%20java.lang.Class)>

--
Thomas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57