Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singular or plural verb in function name? [closed]

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?

like image 453
Gqqnbig Avatar asked Jan 19 '14 23:01

Gqqnbig


People also ask

Should function names start with verbs?

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.

Should the endpoint name be singular or plural?

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.

Should I use plural for table name?

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.


1 Answers

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.

like image 81
Max Noel Avatar answered Sep 29 '22 06:09

Max Noel