IsRunning() or GetIsRunning()?

Discussion in 'Java' started by howa, Feb 21, 2007.

  1. howa

    howa Guest

    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....
     
    howa, Feb 21, 2007
    #1
    1. Advertising

  2. howa

    Lew Guest

    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
     
    Lew, Feb 21, 2007
    #2
    1. Advertising

  3. howa

    Oliver Wong Guest

    "howa" <> wrote in message
    news:...
    >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
     
    Oliver Wong, Feb 21, 2007
    #3
  4. 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/
     
    Knute Johnson, Feb 21, 2007
    #4
  5. howa

    howa Guest


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


    Yes, this perhaps is the main concern...
     
    howa, Feb 21, 2007
    #5
  6. 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.
     
    Andreas Leitgeb, Feb 21, 2007
    #6
  7. 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.lang.String,%20java.lang.Class)>

    --
    Thomas
     
    Thomas Fritsch, Feb 21, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley
    Replies:
    4
    Views:
    3,485
    Tim Williams (gmail)
    Jun 29, 2004
Loading...

Share This Page