Take Java standard library as an example, why is not Object.equals
called Object.equal? And why is Object.clone
used singular from, not Object.clones?
Why do I see almost all people use getXXX
and setXXX
in reading and setting properties of an object, not gets or sets?
Save this answer. Show activity on this post. One of the more universal, yet simple rules is: Function names should be verbs if the function changes the state of the program, and nouns if they're used to return a certain value.
Plural Nouns Are Preferable, Rather Than Singular Using plural or singular nouns for defining resources has no any impact on how your API will work; however, there are common conventions that are used in all good RESTful APIs. One of such conventions is the use of plural nouns for defining resources.
When naming tables, you have two options – to use the singular for the table name or to use a plural. My suggestion would be to always go with names in the singular. If you're naming entities that represent real-world facts, you should use nouns. These are tables like employee, customer, city, and country.
Those method names are verbs, not nouns. equals
is the third-person form of the verb "equal" (likely interrogative -- in languages that support non-alphanumeric characters in method names it's often called equals?
), and clone
is the second-person imperative form of the verb "clone" (you're ordering the object: "clone yourself!").
In general, you use interrogative verbs for methods that query information about the object state relative to another (equals
, contains
, hasXXX
...), imperative verbs for "action" methods (methods that ask the object to perform some task, e.g. clone
, add
, show
... Very often these methods don't return anything), and nouns for methods that query information about the object's state without referencing anything external (e.g. length
, intValue
, keys
). Note that those nouns can be singular or plural depending on what the method returns: singular for something that can only be singular (length
), plural for something that can return a list/map/other container (keys
).
Getters are a special case, where you use an action verb (get) to "tag" these methods as getters, and for symmetry with setters that use set.
Also, getters/setters are a workaround for the lack of properties in C++ and Java. In a language without that weakness, they don't exist: you use properties instead, and those are nouns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With