Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Quick Question regarding method names

Reply
Thread Tools

Quick Question regarding method names

 
 
biro
Guest
Posts: n/a
 
      08-18-2003
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 ?




 
Reply With Quote
 
 
 
 
Jacob
Guest
Posts: n/a
 
      08-18-2003
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())


 
Reply With Quote
 
 
 
 
Tom McGlynn
Guest
Posts: n/a
 
      08-18-2003
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

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-19-2003
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.
 
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick Question regarding Frames Chris S Python 6 03-30-2006 06:43 PM
Quick question, hopefully quick answer. ~misfit~ NZ Computing 114 01-06-2005 01:36 PM
quick question regarding 2620 cisco routers and the processor revision... zenzibar Cisco 1 06-14-2004 04:42 AM
Quick Question Quick Answer JKop C++ 11 05-24-2004 09:46 PM



Advertisments