File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Java method naming conventions: Too many getters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java method naming conventions: Too many getters" Watch "Java method naming conventions: Too many getters" New topic
Author

Java method naming conventions: Too many getters

Arthur Huxley
Greenhorn

Joined: Dec 04, 2009
Posts: 4
Why do Java method names use the "get" prefix so extensively? At least in my Java programs there are a lot of methods with names starting with the word "get". The percentage of get-methods is suspiciosly high. I am starting to feel that the word "get" is loosing it's meaning because of inflation. It is noise in my code.

I have noticed that there is a different naming convention being used in functional/declarative programming and PL/SQL. The method name simply states what the method returns. Instead of "account.getAmount()" or "Time.getIsoFormattedDateString(Date date)" they will use "account.amount()" and "Time.isoFormattedDateString(Date date)". This makes perfect sense to me, as the name of the function describes the result of evaluating the method (assuming there are no side effects, which there shouldn't be anyway). The "get" prefix seems superfluos.

I have just started reading the book "Clean Code". It says that methods should do only one thing, and that that thing should normally be one of the following:

Notify some object about an event, typically passing the event as a parameter.
Ask a question about some object, typically with the method name forming a natural language statement, passing the object as parameter and returning a boolean.
Fetch something, possibly passing some lookup key or some object to be converted as parameter and always returning the desired object/value.
My question is about the third category. Are there naming conventions other than "get" for this kind of methods? What criteria do you use when choosing method names/prefixes?

Here is an example:

I have a class with two methods getDates() and getSpecialDates(). getDates() simply returns the value of a private variable (the reference to a collection of dates). This is a standard getter, as I understand it. getSpecialDates() is different; it calls getDates(), fetches a filter from another class, applies the filter and returns what is effectively a subset of getDates().

The method getSpecialDates() could be named computeSpecialDates), findSpecialDates(), selectSpecialDates() or elicitSpecialDates() or whatever. Or I could simply name it specialDates(). And then, for consistency, I could rename getDates() into dates().

Why bother separating between methods that should be prefixed with "get" and methods that should not, and why bother finding replacement words for "get"?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

get and set are part of the JavaBeans specification.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Normally the Fetching methods (methods that do fetch some values) get the "get" prefix. Its up to you what you want to use. getValue() or fetchValue() or something else. But get and set have become industry standards. So that is why they are commonly used. Plus it makes the objective of the methods easy to understand and we try to make method names clear in what they do.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36482
    
  16
If you don't have a field called specialDates, then you would not usually have a method called getSpecialDates. "get" and "set" methods are usually called after the corresponding fields.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Please BeForthrightWhenCrossPostingToOtherSites

http://stackoverflow.com/questions/3211162/java-method-naming-conventions-too-many-getters

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Try this : Consider 'get' 'set' and 'is' to be an informal part of the Java language
specification. It becomes more formal in JavaBeans land. There is no overuse
problem with 'void' or 'public' so just think of 'get' as another keyword with a
specific intended use. If you avoid using 'set' and 'get' there will be no compiler
error and no runtime error, just a "ConfusedAboutTheCode" exception thrown
by other programmers.

Jim ... ...


BEE MBA PMP SCJP-6
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Campbell Ritchie wrote:If you don't have a field called specialDates, then you would not usually have a method called getSpecialDates. "get" and "set" methods are usually called after the corresponding fields.

I'm not so sure about that--the getters are what expose property retrieval, regardless of whether or not there is a concrete underlying property. For example, a web app class might expose data to the view layer via getters, but the exposed values might be calculated/transformed/filtered/etc. but need to follow the naming convention for access reasons.
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 383

well getters and setters are basically used to get and set data ... as usual

but the more important part of them is to format the data when it is being set or get or apply some filtering or something ... and also it's a way to hide the private data from the other class accessing it so that it doesn't makes any unwanted change to the private data and spoil everything you worked on so long and hard ...

you might just use them to set or get the data or you can do some good stuff inside them ... it's upto you


http://plainoldjavaobject.blogspot.in
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java method naming conventions: Too many getters
 
Similar Threads
JDK and method naming as described in various Java coding standard
beans and jsp and not sure
set Object direct or indirect?
Multiple Values in JSP
Date functionality URGENT!!