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"?
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.
Joined: Oct 13, 2005
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.
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.
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.
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