Some advice on method naming...

H

harry

I have the following method in a java bean class -

private boolean saveReturnPart(List<WarningMessageVO> messages)
{
boolean ret;

// logic here determines if a Return Part should be saved and sets "ret"
variable

return ret;
}

The method basically returns true if a return part should be saved, false if
not - the problem is (and probably a very stupid question) what should i
name the method?

isSaveReturnPart() - doesn't sound right

saveReturnPart() - sounds like it's doing the actual save

shouldSaveReturnPart() - sounds right but is "should" the word to use?

is there a standard for this? - any suggestions?

thanks in advance

harry
 
A

Arved Sandstrom

harry said:
I have the following method in a java bean class -

private boolean saveReturnPart(List<WarningMessageVO> messages)
{
boolean ret;

// logic here determines if a Return Part should be saved and sets "ret"
variable

return ret;
}

The method basically returns true if a return part should be saved, false
if not - the problem is (and probably a very stupid question) what should
i name the method?

isSaveReturnPart() - doesn't sound right

saveReturnPart() - sounds like it's doing the actual save

shouldSaveReturnPart() - sounds right but is "should" the word to use?

is there a standard for this? - any suggestions?

thanks in advance

harry

'is' is the semi-standard prefix for boolean methods...that is, you see if
that'll work for you first. But 'has', 'can' and 'should' are also perfectly
acceptable, because they all imply a truth value.

Your last suggestion is fine - it expresses the desired idea exactly.

AHS
 
H

harry

Arved Sandstrom said:
'is' is the semi-standard prefix for boolean methods...that is, you see if
that'll work for you first. But 'has', 'can' and 'should' are also
perfectly acceptable, because they all imply a truth value.

Your last suggestion is fine - it expresses the desired idea exactly.

AHS

thanks for that Arved, jsut needed someone to give me a sanity check!
 
J

Jean-Baptiste Nizet

harry a écrit :
I have the following method in a java bean class -

private boolean saveReturnPart(List<WarningMessageVO> messages)
{
boolean ret;

// logic here determines if a Return Part should be saved and sets "ret"
variable

return ret;
}

The method basically returns true if a return part should be saved, false if
not - the problem is (and probably a very stupid question) what should i
name the method?

isSaveReturnPart() - doesn't sound right

saveReturnPart() - sounds like it's doing the actual save

shouldSaveReturnPart() - sounds right but is "should" the word to use?

is there a standard for this? - any suggestions?

Hi.

If the method was a property accessor and should conform to the standard
JavaBeans naming conventions, the "is" prefix would be mandatory. I
would then choose a name like isReturnPartSaveNeeded(). But since this
is a simple method, which is private BTW, shouldSaveReturnPart is
perfect: it says exactly what themethod does.

JB.
 
M

Maarten Bodewes

Jean-Baptiste Nizet said:
harry a écrit :

Hi.

If the method was a property accessor and should conform to the standard
JavaBeans naming conventions, the "is" prefix would be mandatory. I
would then choose a name like isReturnPartSaveNeeded(). But since this
is a simple method, which is private BTW, shouldSaveReturnPart is
perfect: it says exactly what themethod does.

I do think that getSaveReturnPart() would also be acceptable. I don't
use beans much, but maybe that's an advantage for questions like this.

Regards,
Maarten
 
D

Daniel Pitts

Patricia said:
Of your ideas, I prefer "shouldSaveReturnPart". Personally, I would use
"doSaveReturnPart", but I think that is just a matter of taste.

Patricia
I disagree. While it is somewhat a matter of choice, its also a style of
communication.
To me, shouldFoo() implies a test to determine whether to call doFoo().
I would expect constructs like:
if (shouldFoo()) {
doFoo();
}

If only Java had allowed "?" as part of an identifier.
if (saveReturnPart?()) {
saveReturnPart();
}
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top