Quick Question regarding method names

Discussion in 'Java' started by biro, Aug 18, 2003.

  1. biro

    biro Guest

    Lets say I have a class called Order and I want a method to cancel the
    order. Would you call this

    cancel() or cancelled()

    ?

    Similarly,

    authorise() or authorised()


    What's the best style ?
     
    biro, Aug 18, 2003
    #1
    1. Advertising

  2. biro

    Jacob Guest

    biro wrote:
    > Lets say I have a class called Order and I want a method to cancel the
    > order. Would you call this
    >
    > cancel() or cancelled()
    >
    > ?
    >
    > Similarly,
    >
    > authorise() or authorised()
    >
    >
    > What's the best style ?


    Method names should be verbs.
    So cancel() (or setCancelled())
    and authorize() (or setAutorized())
     
    Jacob, Aug 18, 2003
    #2
    1. Advertising

  3. biro

    Tom McGlynn Guest

    biro wrote:

    > Lets say I have a class called Order and I want a method to cancel the
    > order. Would you call this
    >
    > cancel() or cancelled()
    >
    > ?
    >
    > Similarly,
    >
    > authorise() or authorised()
    >
    >
    > What's the best style ?
    >


    If your methods change the state of the object, then a verb
    seems appropriate. If it just interrogates the state of the object,
    then use the adjectival form.

    So for me

    void cancel()

    conveys something that actually cancels an order while

    boolean cancelled()

    returns a flag indicating if the order was cancelled, but
    doesn't change the state of the object.

    You might want to have both methods in a given class. Some
    people seem to prefer something like isCancelled for
    the second case, and sometimes that form may be clearer, but in
    this case I wouldn't think it necessary.

    Regards,
    Tom McGlynn
     
    Tom McGlynn, Aug 18, 2003
    #3
  4. biro

    Roedy Green Guest

    On Tue, 19 Aug 2003 01:05:08 GMT, "Miguel De Anda"
    <_sodamnmad_@_hotmail_._com_> wrote or quoted :

    >Is there a reason why booleans use is_ instead of get_? It just seems like
    >an extra thing to remember, and I'm tired of remembering.


    Booleans use is, others use get. Makes code read more logically.

    if ( x.isBloggable() ) "if x is Bloggable"

    rather than
    if ( x.getBloggable() )

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Aug 19, 2003
    #4
    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. JKop
    Replies:
    11
    Views:
    937
  2. John Lenton
    Replies:
    0
    Views:
    389
    John Lenton
    Jan 11, 2005
  3. Chris S

    Quick Question regarding Frames

    Chris S, Mar 28, 2006, in forum: Python
    Replies:
    6
    Views:
    287
    Dave Mandelin
    Mar 30, 2006
  4. Replies:
    5
    Views:
    303
    Bo Persson
    Oct 10, 2006
  5. Steven Burn

    Quick Q regarding querystrings

    Steven Burn, Apr 27, 2004, in forum: ASP General
    Replies:
    10
    Views:
    191
    Steven Burn
    Apr 27, 2004
Loading...

Share This Page