Methods usage analysis
The String
primitive wrapper object is selected to research how some of its methods perform on the instance to understand intuitiveness and consistency in native objects. Below are possible ways methods work on an instance broken down into nine approaches, and each contains a list of words picked from examples for a better understanding of the naming methodology and further analysis.
Primitive value part
The text contains the keyword 'part of the primitive value', and the following example clarifies its meaning.
The primitive value consists of prefix
, text
, and suffix
properties, and each of them is a part of the primitive value.
Approaches
One
The method without parameters gets the primitive value and returns a primitive value.
There are two valueOf()
and toString()
methods that match the description.
Words
value
(noun)to
(preposition)Of
(preposition)String
(noun)
Two
The method without parameters gets the primitive value and performs an action on it based on the intuitive method name, and returns the changed primitive value.
There are among others big()
, blink()
, bold()
, fixed()
, italics()
, small()
, strike()
, sub()
, sup()
, toLocaleLowerCase()
, toLocaleUpperCase()
, toUpperCase()
, trimEnd()
, trimLeft()
, trimRight()
, trimStart()
methods.
For example, the deprecated bold()
method gets the primitive value of a text
string object and returns a modified string
tagged by the Html <b>
tag.
Words
Three
The method with parameters gets the primitive value and performs an action on it based on the intuitive method name with the use of its parameters and returns the changed primitive value.
For example, the replace()
method gets the primitive value of the text
string object and returns a modified string
according to the given parameters.
Words
Four
The method without parameters gets part of the primitive value and returns it.
Let's define the Text
object and extend it with the String
to achieve this approach. The method getPrefix()
returns part of the primitive value prefix
, and the getText()
method returns part of the primitive value text
.
Words
Five
The method without parameters checks the existence of the part of the primitive value in the object and returns the result.
Let's define the Text
object and extend it with the String
to achieve this approach. The hasPrefix()
method checks whether the Text
object has an optional prefix, and the method hasText()
seems useless because the text
it's required, but not really.
Words
Six
The method with parameters checks the existence of the part of the primitive value in the object and returns the result.
Words
Seven
The method without parameters checks the existence of the part of the primitive value in the part of the primitive and returns the result.
Let's define the Text
object and extend it with the String
to achieve this approach. The textHasPrefix()
method checks whether the text
includes the prefix
of the primitive value. The same does hasTextPrefix()
method.
The example shows some possibilities of naming the methods and potential conflict with intuitiveness.
Words
Eight
The method with parameters gets the parts of the primitive value to perform action appropriate to the intuitive method name on given method parameter(s).
Let's define the Replacer
object to explain. The replaceInText()
method gets the parts of the object to perform replacing on a given text. The example does not refer directly to the String
method, but a custom method showing a possibility of different usage of the replace()
method.
The last example of the inText()
method indicates the relation between the object and method name because the object's name suggests action. The same we can do with the first approach above.
Words
replace
(verb)In
(preposition)Text
(noun)
Nine
The method without parameters converts the part of the primitive value into another form, for example, object or array.
Words
Conclusion
The main goal is to properly differentiate method names that work on the primitive value from those using the primitive value on the given method's parameters.
Misuse of words with improper order in the method name results in a different meaning and can lead to non-intuitiveness and inconsistency in the method usage and naming. To avoid such problems it is necessary to have a deeper understanding of the words consistent and intuitive.
Last updated