Quick Question regarding method names

B

biro

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 ?
 
J

Jacob

biro said:
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())
 
T

Tom McGlynn

biro said:
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
 
R

Roedy Green

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() )
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top