Method names analysis
This page covers building the method names using the approaches of the page 'methods usage analysis'. Method names consist of selected words, and among others, there are verbs, prepositions, and nouns in a specific order, where can be found intuitiveness and functionality. The methods perform actions, and the first step is to create a small list of them.
Possible actions
There are possible actions that can be performed on an instance, and the following list contains a few general words that are used to build method name structures below.
exec(noun)get (verb) Indicates a direction pick-out.
has (verb) (Indicating possibility) Indicates a direction pick-check-out.
is (a state of being verb) Indicates a direction pick-check-out.
replace (verb) Indicates a direction pick-change-out.
set (verb) Indicates a direction pick-change-in.
to (preposition) Indicates a direction pick-(change)-out.
Approaches
The nine following approaches show possible name structures that clarify the method naming methodology. The method name structure is built from a few simple brackets, with the description and type, like in the following example.
[ action: verb | noun ][ what: noun | verb ]([ parameter: noun ])
One
get
()
get
()
[ get: verb ]()
new Wrap('<', '>', 'span').get(); // Returns <span>
getPrimitive
()
getPrimitive
()
[ get: verb ][ Primitive: noun ]()
new Wrap('<', '>', 'span').getPrimitive(); // Returns <span>
getWrap
()
getWrap
()
[ get: verb ][ Wrap: noun ]()
new Wrap('<', '>', 'span').getWrap(); // Returns <span>
toPrimitive
()
toPrimitive
()
[ to: preposition ][ Primitive: noun ]()
new Wrap('<', '>', 'span').toPrimitive(); // Returns <span>
toString
()
toString
()
[ to: preposition ][ String: noun ]()
new Wrap('<', '>').toString(); // Returns <>
valueOf
()
valueOf
()
[ value: noun ][ Of: preposition ]()
new Wrap('<', '>').valueOf(); // Returns <>
Conclusion
The simple method names with prefixes like a verb, preposition, or even noun return the primitive value of a specified object.
Especially the valueOf()
method name indicates that the noun value
is like a property picked with the following action performed on it. This way of thinking where the method name first word denotes getting the property is helpful in approach eight and approach three.
The actions assigned to this approach are
get
andto
indicate getting the primitive value.The action
get
without the following word indicates the return of the primitive value.
Two
big
()
big
()
[ big: verb ]()
new String('text').big() // Returns <big>text</big>
new Wrap('<', '>').big(); // Returns <big><></big>
bold
()
bold
()
[ bold: verb ]()
new String('text').bold(); // Returns <b>text</b>
new Wrap('<', '>', 'span').bold(); // Returns <b><span></b>
Conclusion
These methods are native methods of the String
object, and their meaning is to transform the primitive value to the bold or big. No words in their names are informing about how they behave. They could have added prepositions, for example, 'to' to get toBig()
or toBold()
, but are they necessary? However, these methods are denoted as deprecated.
It seems better to have a verb instead of a noun in the method name of one word because nouns are reserved names for properties or accessors.
One of the exceptions is the 'wrap' word cause it's a verb and noun.
Three
replace
(
searchValue: string, replaceValue: string
)
replace
(
searchValue: string, replaceValue: string
)
[ replace: verb ]([ searchValue: noun ], [ replaceValue: noun ])
new String('text').replace('e', 'E'); // Returns tExt
new Wrapper('<', '>', 'span').replace('a', 'A'); // Returns <spAn>
wrap
(
opening: string, closing: string
)
wrap
(
opening: string, closing: string
)
[ wrap: verb ]([ opening: noun ], [ closing: noun ])
new Wrapper('<', '>', 'span').wrap('{', '}'); // Returns {<span>}
new Wrapper('', '', 'span').wrap('{', '}'); // Returns {span}
wrapText
(
opening: string, closing: string
)
wrapText
(
opening: string, closing: string
)
[ wrap: verb ][ Text: noun ]([ opening: noun ], [ closing: noun ])
new Wrapper('<', '>', 'span').wrapText('{', '}'); // Returns <{span}>
wrapOpening
(
opening: string, closing: string
)
wrapOpening
(
opening: string, closing: string
)
[ wrap: verb ][ Opening: noun ]([ opening: noun ], [ closing: noun ])
new Wrapper('<', '>', 'span').wrapOpening('{', '}'); // Returns {<}span>
new Wrapper('', '>', 'span').wrapOpening('{', '}'); // Returns {}span>
What should be returned in this method, the primitive value with changed opening or wrapped opening? The following method could be an answer.
openingWrap
(
opening: string, closing: string
)
openingWrap
(
opening: string, closing: string
)
[ opening: noun ][ Wrap: verb ]([ opening: noun ], [ closing: noun ])
// Returns {<}span> or {<}
new Wrapper('<', '>', 'span').openingWrap('{', '}'); // Returns {<}
The action(verb, preposition) following the object indicates performing this action on the primitive value. The noun indicates getting the property to perform on it. The result of this method should be a wrapped opening {>}
.
replaceOpening
(
opening: string
)
replaceOpening
(
opening: string
)
[ replace: verb ][ Opening: noun ]([ opening: noun ])
new Wrapper('<', '>', 'span').replaceOpening('{'); // Returns {span>
new Wrapper('<', '', 'span').replaceOpening('{'); // Returns {span>
new Wrapper('<', '>', 'span').replaceOpening('{{{'); // Returns {{{span>
replaceClosing
(
closing: string
)
replaceClosing
(
closing: string
)
[ replace: verb ][ Closing: noun ]([ closing: noun ])
new Wrapper('<', '>', 'span').replaceClosing('}'); // Returns <span}
new Wrapper('<', '', 'span').replaceClosing('}'); // Returns <span}
new Wrapper('<', '>', 'span').replaceClosing('}}}'); // Returns <span}}}
replaceText
(
text: string
)
replaceText
(
text: string
)
[ replace: verb ][ Text: noun ]([ text: noun ])
new Wrapper('{', '}', 'span').replaceText('div'); // Returns {div}
Conclusion
The actions assigned to this approach are
replace
,wrap
.If the object is followed by a verb the method changes the primitive value or its parts and returns the changed primitive value.
If the object is followed by a noun the method changes the primitive value part and returns it.
Following intuitiveness of native method
replace()
thewrap
method should return the primitive value of theWrapper
after replacing.
Four
Previous approach three teaches that a noun after the verb, for example, replaceText()
returns the primitive value, not part of it. The following examples behave differently cause they return part of the primitive value, having the same method name structure.
getOpening
()
getOpening
()
[ get: verb ][ Opening: noun ]()
new Wrapper('<', '>', 'span').getOpening(); // Returns <
new Wrapper('', '>', 'span').getOpening(); // Returns empty string
getClosing
()
getClosing
()
[ get: verb ][ Closing: noun ]()
new Wrapper('<', '>', 'span').getClosing(); // Returns >
new Wrapper('<', '', 'span').getClosing(); // Returns empty string
getText
()
getText
()
[ get: verb ][ Text: noun ]()
new Wrapper('<', '>', 'span').getText(); // Returns span
new Wrapper('<', '>', '').getText(); // Returns empty string
new Wrapper('<', '>').getText(); // Returns empty string
Conclusion
These method names refer to primitive value parts and return them. We would say the action of get
returns part of the primitive value, but the examples of approach one make it ambiguous.
The action assigned to this approach is
get
.The action
get
following the object name with the following noun indicates return value is a part of the primitive value.Including the approach one action
get
does not indicate the return value, but the following noun does. The actionget
without the following word indicates the return of the primitive value.
Five
hasOpening
()
hasOpening
()
[ has: verb ][ Opening: noun ]()
new Wrap('<', '>', 'span').hasOpening(); // Returns true
new Wrap('', '>', 'span').hasOpening(); // Returns false
hasClosing
()
hasClosing
()
[ has: verb ][ Closing: noun ]()
new Wrapper('<', '>', 'span').hasClosing(); // Returns true
new Wrapper('<', '', 'span').hasClosing(); // Returns false
hasText
()
hasText
()
[ has: verb ][ Text: noun ]()
new Wrapper('<', '>', 'span').hasText(); // Returns true
new Wrapper('<', '>', '').hasText(); // Returns false
new Wrapper('<', '>').hasText(); // Returns false
isWrapped
()
isWrapped
()
[ is: verb ][ Wrapped: noun ]()
new Wrapper('<', '>', 'span').isWrapped(); // Returns true
openingHas()
openingHas()
[ opening: noun ][ Has: verb ]()
new Wrap('<', '>', 'span').openingHas();
The method name without a noun after the verb has
seems useless.
Conclusion
The method names seem to be obvious, instead of the last one. If the words are swapped their meaning is changed. The openingHas()
method indicates the opening has something, and that something should be provided as the method's parameter.
The actions assigned to this approach are
has
,is
.When a verb follows the object and a noun follows the verb, the method checks the part of the primitive value.
When a noun following the object refers to the primitive value part, the following verb indicates perform on it.
Six
hasOpening
(
opening: string
)
hasOpening
(
opening: string
)
[ has: verb ][ Opening: noun ]([ opening: noun ])
new Wrap('<', '>', 'span').hasOpening('<'); // Returns true
new Wrap('<', '>', 'span').hasOpening('{'); // Returns false
new Wrap('', '>', 'span').hasOpening('<'); // Returns false
hasClosing
(
closing: string
)
hasClosing
(
closing: string
)
[ has: verb ][ Closing: noun ]([ closing: noun ])
new Wrapper('<', '>', 'span').hasClosing('>'); // Returns true
new Wrapper('<', '>', 'span').hasClosing('{'); // Returns false
new Wrapper('<', '', 'span').hasClosing('>'); // Returns false
hasText
(
text: string
)
hasText
(
text: string
)
[ has: verb ][ Text: noun ]([ text: noun ])
new Wrapper('<', '>', 'span').hasText('span'); // Returns true
new Wrapper('<', '>', 'span').hasText('div'); // Returns false
new Wrapper('<', '>', '').hasText(''); // Returns false
new Wrapper('<', '>').hasText(''); // Returns false
isWrapped
(
opening: string, closing: string
)
isWrapped
(
opening: string, closing: string
)
[ is: verb ][ Wrapped: noun ]([ opening: noun, closing: noun ])
new Wrapper('<', '>', 'span').isWrapped('<', '>'); // Returns true
new Wrapper('<', '>', 'span').isWrapped('{', '>'); // Returns false
new Wrapper('<', '>', 'span').isWrapped('<', '}'); // Returns false
new Wrapper('<', '>', '').isWrapped('<', '>'); // Returns true
Conclusion
The actions assigned to this approach are
has
,is
.
Seven
textHasOpening
()
textHasOpening
()
[ text: noun ][ Has: verb ][ Opening: noun ]()
new Wrap('<', '>', 'span').textHasOpening(); // Returns false
new Wrap('<', '>', '<span').textHasOpening(); // Returns true
new Wrap('<', '>', '<span').textHasOpening(); // Returns true
The method checks whether the span
text has the opening <
.
textHasClosing
()
textHasClosing
()
[ text: noun ][ Has: verb ][ Closing: noun ]()
new Wrapper('<', '>', 'span').textHasClosing(); // Returns false
The method checks whether the span
text has the closing >
. Let's add a parameter to the method.
textHasOpening(opening: string)
textHasOpening(opening: string)
[ text: noun ][ Has: verb ][ Opening: noun ]([ opening: noun ])
new Wrap('<', '>', 'span').textHasOpening('<'); // Returns false
new Wrap('<', '>', '<span').textHasOpening('<'); // Returns true
The method gets the text and checks whether it contains the given opening chars.
isTextWrapped
(
opening: string, closing: string
)
isTextWrapped
(
opening: string, closing: string
)
[ is: verb ][ Text: noun ][ Wrapped: noun ]([ opening: noun, closing: noun ])
new Wrapper('<', '>', '<span>').isTextWrapped('<', '>'); // Returns true
new Wrapper('<', '>', '{span}').isTextWrapped('{', '}'); // Returns true
new Wrapper('<', '>', 'span').isTextWrapped('<', '>'); // Returns false
new Wrapper('<', '>', '').isTextWrapped('<', '>'); // Returns false
new Wrapper('<', '>').isTextWrapped('<', '>'); // Returns false
The method checks whether the span
text has the opening <
and closing >
.
Conclusion
The use of such methods is questionable. A noun following the object indicates the obtaining action similar to the openingWrap()
method.
The actions assigned to this approach are
has
,is
.If the object is followed by a noun the method changes the primitive value part and returns it.
Eight
An interesting thing about this approach is, it seems the same as approach 'three' above. It also changes the primitive value, but not exactly, because its idea is to use part of the primitive value to change the given method's parameter.
For example, let's try to wrap the text given in the method's parameter with the opening and closing of the Wrapper
object.
wrapText
(
text: string
)
wrapText
(
text: string
)
[ wrap: verb ][ Text: noun ]([ text: noun ])
// The text is wrapped and returns wrapped text.
new Wrapper('{', '}', 'span').wrapText('div'); // Returns {div}
The wrap()
method from approach three indicates that the object followed by action intuitively means the use method's parameters to wrap the primitive value. Because this approach's way of thinking and conflicted parameters with the wrapText()
method from approach three, the wrapText()
method is not intuitive.
We could think the method wraps the text span
with the provided opening div
, as in approach 'three' example above. However, this forces to achieve that the div
text is wrapped by the opening {
and closing }
resulting {div}
. The method name should indicate its functionality.
The method works like a replace()
with the exact meaning of a specific replace in name, and it should return a changed primitive value, like the replaceText()
method.
Even by swapping the words, the method name is still not intuitive. Cause of thinking that the text span
is picked from the object, and the action Wrap
is performed on it.
textWrap
(
text: string
)
textWrap
(
text: string
)
[ text: noun ][ Wrap: verb ]([ text: noun ])
// Returns divspan or {div}
new Wrapper('{', '}', 'span').textWrap('div'); // Returns {div}
The question is how to properly differentiate method names to achieve intuitiveness and consistency in method naming for both approaches. The method's name should indicate the use of opening and closing chars to wrap the text given in the method's parameter.
The meaningful solution is the use of in
and on
prepositions, then the methods meaning should indicate use of primitive value parts.
For example, adding at the end of the replaceOpening
name the In
preposition results in the replaceOpeningIn
method name. The name suggests replacing the opening in something, and the something is the given text.
replaceOpeningIn
(
text: string
)
replaceOpeningIn
(
text: string
)
[ replace: verb ][ Opening: noun ][ In: preposition ]([ text: noun ])
new Wrapper('{', '}', 'span').replaceOpeningIn('{div}', '<'); // Returns <div}
Use the On
preposition after the verb Wrap
should intuitively indicate wrap on something outside.
wrapOn(text: string)
wrapOn(text: string)
[ wrap: noun ][ On: preposition ]([ text: noun ])
new Wrapper('<', '>', 'span').wrapOn('div'); // Returns
Crucial question is what the wrapOn()
method should return, the Wrap
instance or the primitive value. Because the Wrapper
contains all the needed properties it seems no sense to return the Wrap
instance and better to return the primitive value.
isClosingIn
(
text: string
)
isClosingIn
(
text: string
)
[ is: verb ][ Closing: noun ][ In: preposition ]([ text: noun ])
new Wrapper('{', '}', 'span').isClosingIn('{div}'); // Returns true
new Wrapper('{', '>', 'span').isClosingIn('{div}'); // Returns false
new Wrapper('{', '', 'span').isClosingIn('{div'); // Returns false
The method checks whether the given text has the closing of the Wrapper
instance. It's possible to get part of the primitive value first, and then perform an action on it in the direction specified by the In
preposition with the name closingIsIn
.
closingIsIn
(
text: string
)
closingIsIn
(
text: string
)
[ closing: noun ][ Is: verb ][ In: preposition ]([ text: noun ])
new Wrapper('{', '}', 'span').closingIsIn('{div}'); // Returns true
new Wrapper('{', '>', 'span').closingIsIn('{div}'); // Returns false
new Wrapper('{', '', 'span').closingIsIn('{div'); // Returns false
The method checks whether the picked closing of the Wrapper
instance is in the given text.
Conclusion
Prepositions indicate directions.
Intuitively an action without a noun following the object indicates the change on the primitive value, but the following noun indicates the pick of the primitive value part for possible perform change.
There is a constant value in the
Wrapper
object, and the difference is in the method's parameters. TheWrapper
can wrap multiple texts or wrap the part of the primitive value text with multiple opening and closing chars.
Nine
toArray
()
toArray
()
[ to: preposition ][ Array: noun ]()
new Wrapper('<', '>', 'span').toArray(); // Returns ['<', 'span', '>']
toWrap
()
toWrap
()
[ to: preposition ][ Wrap: noun ]()
new Wrapper('<', '>', 'span').toArray(); // Returns Wrap {'<span>'}
Conclusion
Analysis of possible structures above gives answers that can help prepare some method names useful in the Wrap
and Wrapper
objects.
I see it also as a material for creating general principles of designing primitive wrapper objects for angular-package.
Last updated
Was this helpful?