Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Some advice on method naming... (http://www.velocityreviews.com/forums/t622856-some-advice-on-method-naming.html)

harry 06-29-2008 10:43 AM

Some advice on method naming...
 
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



Arved Sandstrom 06-29-2008 11:32 AM

Re: Some advice on method naming...
 
"harry" <a@abc.com> wrote in message
news:9HJ9k.176125$1B6.75951@newsfe21.ams2...
>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



harry 06-29-2008 11:48 AM

Re: Some advice on method naming...
 
"Arved Sandstrom" <asandstrom@accesswave.ca> wrote in message
news:ZoK9k.927$7%6.23@edtnps82...
> "harry" <a@abc.com> wrote in message
> news:9HJ9k.176125$1B6.75951@newsfe21.ams2...
>>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
>


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



Jean-Baptiste Nizet 06-29-2008 12:59 PM

Re: Some advice on method naming...
 
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.

> thanks in advance
>
> harry
>
>


Maarten Bodewes 06-29-2008 11:32 PM

Re: Some advice on method naming...
 
Jean-Baptiste Nizet wrote:
> 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.


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

Daniel Pitts 06-30-2008 03:09 PM

Re: Some advice on method naming...
 
Patricia Shanahan wrote:
> 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();
}
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>


All times are GMT. The time now is 05:59 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.