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.
// Define a new `Text` class.
class Text extends string {
constructor(
public prefix: string, // Part of the primitive value.
public text: string, // Part of the primitive value.
public suffix: string // Part of the primitive value.
) {
super(`${prefix}${text}${suffix}`); // The primitive value.
}
}
// Returns <span>
new Text('<', 'span', '>').valueOf();The primitive value consists of prefix, text, and suffix properties, and each of them is a part of the primitive value.
Approaches
One
There are two valueOf() and toString() methods that match the description.
Words
value(noun)to(preposition)Of(preposition)String(noun)
Two
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
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
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
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
Words
Seven
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
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
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
Was this helpful?