Method names analysis
It is worth checking only the method name, not including the object and its functionality, because then it's possible to achieve constant intuitive method names, at first sight, indicating its meaning. The need is also to have the same parameters.
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
The method without parameters gets the primitive value and returns a primitive value.
get
()
get
()
[ get: verb ]()
getPrimitive
()
getPrimitive
()
[ get: verb ][ Primitive: noun ]()
getWrap
()
getWrap
()
[ get: verb ][ Wrap: noun ]()
toPrimitive
()
toPrimitive
()
[ to: preposition ][ Primitive: noun ]()
toString
()
toString
()
[ to: preposition ][ String: noun ]()
valueOf
()
valueOf
()
[ value: noun ][ Of: preposition ]()
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
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.
big
()
big
()
[ big: verb ]()
bold
()
bold
()
[ bold: verb ]()
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
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.
replace
(
searchValue: string, replaceValue: string
)
replace
(
searchValue: string, replaceValue: string
)
[ replace: verb ]([ searchValue: noun ], [ replaceValue: noun ])
wrap
(
opening: string, closing: string
)
wrap
(
opening: string, closing: string
)
[ wrap: verb ]([ opening: noun ], [ closing: noun ])
wrapText
(
opening: string, closing: string
)
wrapText
(
opening: string, closing: string
)
[ wrap: verb ][ Text: noun ]([ opening: noun ], [ closing: noun ])
wrapOpening
(
opening: string, closing: string
)
wrapOpening
(
opening: string, closing: string
)
[ wrap: verb ][ Opening: noun ]([ opening: noun ], [ closing: noun ])
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 ])
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 ])
replaceClosing
(
closing: string
)
replaceClosing
(
closing: string
)
[ replace: verb ][ Closing: noun ]([ closing: noun ])
replaceText
(
text: string
)
replaceText
(
text: string
)
[ replace: verb ][ Text: noun ]([ text: noun ])
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
The method without parameters gets part of the primitive value and returns it.
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 ]()
getClosing
()
getClosing
()
[ get: verb ][ Closing: noun ]()
getText
()
getText
()
[ get: verb ][ Text: noun ]()
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
The method without parameters checks the existence of the part of the primitive value in the object and returns the result.
hasOpening
()
hasOpening
()
[ has: verb ][ Opening: noun ]()
hasClosing
()
hasClosing
()
[ has: verb ][ Closing: noun ]()
hasText
()
hasText
()
[ has: verb ][ Text: noun ]()
isWrapped
()
isWrapped
()
[ is: verb ][ Wrapped: noun ]()
openingHas()
openingHas()
[ opening: noun ][ Has: verb ]()
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
The method with parameters checks the existence of the part of the primitive value in the object and returns the result.
hasOpening
(
opening: string
)
hasOpening
(
opening: string
)
[ has: verb ][ Opening: noun ]([ opening: noun ])
hasClosing
(
closing: string
)
hasClosing
(
closing: string
)
[ has: verb ][ Closing: noun ]([ closing: noun ])
hasText
(
text: string
)
hasText
(
text: string
)
[ has: verb ][ Text: noun ]([ text: noun ])
isWrapped
(
opening: string, closing: string
)
isWrapped
(
opening: string, closing: string
)
[ is: verb ][ Wrapped: noun ]([ opening: noun, closing: noun ])
Conclusion
The actions assigned to this approach are
has
,is
.
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.
textHasOpening
()
textHasOpening
()
[ text: noun ][ Has: verb ][ Opening: noun ]()
The method checks whether the span
text has the opening <
.
textHasClosing
()
textHasClosing
()
[ text: noun ][ Has: verb ][ Closing: noun ]()
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 ])
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 ])
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
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).
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 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 ])
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 ])
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 ])
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 ])
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 ])
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
The method without parameters converts the part of the primitive value into another form, for example, object or array.
toArray
()
toArray
()
[ to: preposition ][ Array: noun ]()
toWrap
()
toWrap
()
[ to: preposition ][ Wrap: noun ]()
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