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)
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this communityDo 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".
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
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.