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, and here's a couple of things to discuss.
Number of nouns
First look at the quantity. In order to improve readability, the number of singular and plural nouns has a significant difference in semantics and needs to be carefully considered. Like what:
Properties: System.collections.generic.dictionary<tkey, Keys and count in tvalue> are good examples. The field is similar to this; the noun in the method name: We are familiar with getElementById and getElementsByTagName in JS;
Local variables: We are more likely to encounter local variables than properties. 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 list<string> 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, take a look at an example in the. NET framework. In System.collections.generic.stack<t>, there is a contains (T item) method for such code:
- if (Thestack.contains (1))
- {
- Do something.
- }
It reads like this: If the stack contains 1, it's 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. Let's write code first.
- 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 also follow, in the. NET framework, using the third person, whether for type or type members. Other common annotations should also follow this rule.
Original link: http://www.cnblogs.com/anderslly/archive/2011/01/26/name-conventions.html
"Edit Recommendation"