We all understand the importance of naming, and if we are constantly concerned about naming, we need to consider the names of verbs and nouns, in particular the number of person and noun of the verb.
In the guideline of the. NET framework, there is a special chapter that discusses naming conventions, including capitalization, word selection, and so on, but not in terms of person and quantity, which is discussed below.
Number of nouns
First look at the quantity. In order to improve readability, the number of singular and plural nouns have a significant difference in semantics. Like what:
Properties: System.Collections.Generic.DictionaryTKey, Keys and count in TValue are good examples. A field is similar to this; a noun in a method name: The getElementById and getElementsByTagName we are familiar with in JS; Local variables: We are more likely to encounter local variables than attributes. For example, using a variable to represent a person's name, you can use string name; If it is more than one person's name, you can use liststring names.
In general, the semantics of singular expressions are? (0 or 1); The semantics of complex expressions are * (0, 1, or more). The number of nouns is easy to understand, and the relevant rules are easy to follow.
The person of the verb
Next consider the verb's person. In objects that need to be considered for naming, including namespaces, types, and various types of members, only the method is a verb, semantically representing an action. About it, look at an example in the. NET framework. In System.Collections.Generic.StackT, there is a contains (T item) method for such code:
if (Thestack.contains (1)) {//do something.}
Read like: If the stack contains 1, more fluent. Are all methods to be named like this? No. The other two methods clear () and push (T item) are not so, this place is very confusing. First write the code to see:
Thestack.push (3); Thestack.clear ();
Try to read as a sentence as above: the stack push 3; The stack clear, grammatically incorrect, and seems to be the third person. I'm not sure why, but I can only try to explain. After examining multiple collection types and other types, it is found that all places using the third person are predicate functions, in addition to the CONTAINS (T item) and Directory.Exists (string path), do not know if this is the naming convention?
Also consider notes
To some extent, annotations are also a kind of code. One is the XML document annotation, which we can follow, in the. NET framework, using the third person, whether for type or type members. Other common annotations should also follow this rule.