3

Do the words to, as and get have any special meaning when used in method names?

Examples

to

new Integer(4).toString()

get

Integer.getInteger("system.unknown.property",4)

as

Arrays.asList(myPrimitiveArray)
12

The term as is often used to denote that the method casts the item from one type to another, without creating a new object.

The term to often indicates that the method creates a new representation of the value as a new object of a different type.

The term get, along with set in Java EE has a special meaning as getters and setters, which are simply access and modify methods respectively to a private field. However, there does sometimes exist the convention in a wider sense of get indicating an existing value is returned and create indicates a new value is created.

And then there are many many situations where none of the above applies and as is used to create new objects, to just does a cast, get creates something etc.

So beyond get in the Java EE context, the answer to "do the words to, as and get have any special meaning when used in method names?", the answer is "sometimes".

  • sometimes in in other words, It depends on the developer's ability to name things. So, no, they don't have special meaning but the one the developer had in mind. Your answer sounds a convention to me, and I like It, however I have never read it as a Java naming/style convention. Do you have any reference or link to share? – Laiv Apr 25 '18 at 6:31
  • @Laiv, Having had a look, it may well be a convention that I've picked up over time. For example, looking at the link that Deduplicator provides in his comment to the question has answers that are all over the place. – David Arno Apr 25 '18 at 8:26
  • Good enough. Looks like there's a sort of agreement around the premises you introduced. I gather this sort of "resources" for me to have some guidelines to follow during the development. Thank you! – Laiv Apr 25 '18 at 8:35
  • Good answers. And it can get murky if you've got a method that may have to dynamically decide whether to create a new object (of the desired type) or just a cast (such as returning a List from an Iterable object which happens to already be a List). This happens plenty in static factory methods. "from": You also see .from(Type) or especially if there are variant static factory/constructor methods that need to distinguish different meanings for parameters: .fromSlopeIntercept(double,double) and .fromRadiusAngle(double,double) – George Forman May 3 '20 at 12:04
0

According to naming conventions, prefix of any variable/method name/class name should like in such a way so that it automatically describes its usage.

You should use as keyword as a prefix in the method name when you want to convert particular object or variable of specific type of data into the other type if data. i.e. type casting.

You should use to keyword as a prefix in the method name when you want to show an object or variable in the different type without converting it from its original datatype.

You should use get keyword as a prefix in the method name either it is a get method from getter / setter or else you are retrieving data from the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.