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)
Do the words to, as and get have any special meaning when used in method names?
new Integer(4).toString()
Integer.getInteger("system.unknown.property",4)
Arrays.asList(myPrimitiveArray)
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".
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.