Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Method naming convention required

Reply
Thread Tools

Method naming convention required

 
 
Xagyg
Guest
Posts: n/a
 
      10-20-2006
In Ruby, a method that modifies the instance using it is suffixed with
a '!'.

E.g. someString.reverse() returns a String with a reversed result,
however, someString.reverse! reverses the result of someString.

Does anyone have/use/know of a method-naming convention in Java to
distinguish between methods that modify the instance and methods that
don't?

Feel free to suggest one if you like.

 
Reply With Quote
 
 
 
 
Xagyg
Guest
Posts: n/a
 
      10-20-2006

>
> E.g. someString.reverse() returns a String with a reversed result,
> however, someString.reverse! reverses the result of someString.


> reverses the result of someString.


I meant "reverses someString" in-place.

 
Reply With Quote
 
 
 
 
Daniel Dyer
Guest
Posts: n/a
 
      10-20-2006
On Fri, 20 Oct 2006 05:45:37 +0100, Xagyg <(E-Mail Removed)> wrote:

>> E.g. someString.reverse() returns a String with a reversed result,
>> however, someString.reverse! reverses the result of someString.

>
>> reverses the result of someString.

>
> I meant "reverses someString" in-place.


I'm not aware of any naming convention like this in Java. However, if you
are only concerned with Strings, it's impossible(*) to reverse them in
place anyway since they are immutable.
[*] Almost.

Dan.

--
Daniel Dyer
http://www.dandyer.co.uk
 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      10-20-2006
Xagyg wrote:
> Does anyone have/use/know of a method-naming convention in Java to
> distinguish between methods that modify the instance and methods that
> don't?


The attribute setter/getter convention taken from the JavaBeans spec
http://java.sun.com/j2se/1.5.0/docs/...ans/index.html is probably
closest:

void setSomething(type value)
type getSomething()

That convention is largely observed, even for non-JavaBean-compliant
classes. One can argue it is even often overused.

Other stuff is defined chapter 10 of

http://java.sun.com/docs/codeconv/index.html

/Thomas
--
The comp.lang.java.gui FAQ:
http://gd.tuwien.ac.at/faqs/faqs-hie...lang.java.gui/
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
 
Reply With Quote
 
Xagyg
Guest
Posts: n/a
 
      10-20-2006

Thomas Weidenfeller wrote:

> The attribute setter/getter convention taken from the JavaBeans spec
> http://java.sun.com/j2se/1.5.0/docs/...ans/index.html is probably
> closest:
>
> void setSomething(type value)
> type getSomething()
>
> That convention is largely observed, even for non-JavaBean-compliant
> classes. One can argue it is even often overused.


I'll give another example. I want my Set class to have 2 "union"
methods. The first method unions another set to the caller. The second
one unions another set and returns a new set, but the caller is not
modified.

e.g. this.union(s) modifies this, whereas the other version of
this.union(s) does NOT modify this, it returns a new set containing the
union of this and s. It might seem a little strange to have setUnion(s)
and getUnion(s) since the set and get are actually doing extra
processing ... not just setting and getting some object. I like the
suggestion though - it is based on a sound principle. Not sure it feels
right though.

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      10-20-2006
Xagyg wrote:

> I'll give another example. I want my Set class to have 2 "union"
> methods. The first method unions another set to the caller. The second
> one unions another set and returns a new set, but the caller is not
> modified.


Call the second union(), but don't call the first one that, since that's not
what "union" means. If it fits with your framework then use the established
name addAll() instead.

(addAll() is part of the contract defined by the java.util.Collection
interface.)

A more general point: I think that one good way to distinguish between methods
which modify the target object and those which answer a modified version is to
(try to) follow the English pattern: verb/verbed. So that the string reversal
example would have methods called reverse() (which modified the string in
place), and reversed() which returned a reversed copy. Of course you can go
beyond that into, say, beReversed() vs reversed(), or even reverse() vs
copyReversed().

It's a bit unfortunate that there isn't a Lisp/Ruby style qualifier character
available, but there isn't. And also there is no established convention, so we
just have to do the best we can with well chosen method names (fighting the
oddities of English grammar the while...)

-- chris



 
Reply With Quote
 
Xagyg
Guest
Posts: n/a
 
      10-20-2006

Chris Uppal wrote:

> Xagyg wrote:
>
> > I'll give another example. I want my Set class to have 2 "union"
> > methods. The first method unions another set to the caller. The second
> > one unions another set and returns a new set, but the caller is not
> > modified.

>
> Call the second union(), but don't call the first one that, since that's not
> what "union" means. If it fits with your framework then use the established
> name addAll() instead.
>
> (addAll() is part of the contract defined by the java.util.Collection
> interface.)
>
> A more general point: I think that one good way to distinguish between methods
> which modify the target object and those which answer a modified version is to
> (try to) follow the English pattern: verb/verbed. So that the string reversal
> example would have methods called reverse() (which modified the string in
> place), and reversed() which returned a reversed copy. Of course you can go
> beyond that into, say, beReversed() vs reversed(), or even reverse() vs
> copyReversed().
>
> It's a bit unfortunate that there isn't a Lisp/Ruby style qualifier character
> available, but there isn't. And also there is no established convention, so we
> just have to do the best we can with well chosen method names (fighting the
> oddities of English grammar the while...)
>
> -- chris


Nice post. Thanks. I'll work with something along these lines.

 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      10-20-2006
Xagyg wrote:
> In Ruby, a method that modifies the instance using it is suffixed with
> a '!'.
>
> E.g. someString.reverse() returns a String with a reversed result,
> however, someString.reverse! reverses the result of someString.
>
> Does anyone have/use/know of a method-naming convention in Java to
> distinguish between methods that modify the instance and methods that
> don't?


The convention is that mutable objects stick with methods that mutate
themselves. Equivalent methods return results without modifying the
source objects belong to immutables.

Tom Hawtin
 
Reply With Quote
 
Doug Pardee
Guest
Posts: n/a
 
      10-20-2006
Xagyg wrote:
> Does anyone have/use/know of a method-naming convention in Java to
> distinguish between methods that modify the instance and methods that
> don't?


Not a naming convention, but a signature convention:
If the method has a return value, it doesn't modify anything.
If the method modifies anything, it doesn't have a return value.

This is called Command/Query Separation. CQS is generally considered to
be a good thing to do, although there are a few isolated instances
where there is real value in returning a value from a method that
modifies things.

Virtually all programming languages prior to C strictly separated
statements from expressions, subroutines from functions. C introduced
side-effect functions, statement expressions, and expression
statements, and derivative languages like Java have followed suit. But
just because a language allows you to do something and doing so saves
you ten keystrokes, that doesn't mean that it's a good idea in terms of
code quality.

(Most languages couldn't actually stop you from putting a side effect
into a function definition, but it wasn't something that was routinely
done, particularly if there was a chance that the compiler might
optimize the function calls.)

 
Reply With Quote
 
Mark Rafn
Guest
Posts: n/a
 
      10-20-2006
Thomas Hawtin <(E-Mail Removed)> wrote:
>> Does anyone have/use/know of a method-naming convention in Java to
>> distinguish between methods that modify the instance and methods that
>> don't?


How I wish the language supported this! There's probably no reason not to add
an annotation (@Const is probably too controversial, but @ReadOnly could work)
to indicate methods that don't change any values.

>The convention is that mutable objects stick with methods that mutate
>themselves. Equivalent methods return results without modifying the
>source objects belong to immutables.


Note that this convention is not universal, even within the JDK.
StringBuilder, for instance, mutates and returns a copy itself for easy
chaining of calls.

It's a good idea to follow it when you can, but there's no subsitute for
understanding the API you're calling and knowing which methods read vs write.
--
Mark Rafn http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.dagon.net/>
 
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
About method naming convention with "!" Paul A. Ruby 2 06-27-2010 09:52 PM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
WS C3550 naming convention question frishack@gmail.com Cisco 2 02-23-2005 07:29 PM
JSP Method Naming Convention Buck Turgidson Java 1 03-02-2004 04:43 PM
Which c# naming convention? Rick ASP .Net 1 01-19-2004 11:42 AM



Advertisments