Good naming rules can contribute to the Cooperative Development of teams, and play a crucial role in both project development and product maintenance. It should be said that naming conventions are a convention and a bridge of good communication between programmers. In addition, the ancients believed that as long as they knew the real name of a person, they would gain incredible power over that person. As long as you think of the right name for a thing, it can bring more power than the code. If all the names are suitable for their nature, the relationship is clear, and the meaning can be deduced. The assumption of the average person can also be expected.
The naming convention is introduced in VB:
• Controls and variables are often referenced in program code. In a complex process, it is difficult to fully understand all the elements in the code. • How can we determine whether a statement references a variable or a control? Without some conventions that separate variables from controls, the code is hard to read. • Example:
TotalDue=LineItemTotal-Discount+TotalTax
• Software developers should change from a data type suffix to a single character prefix
• Example
–Dim Iwidth As Long –Dim iHeight As Integer)
• A Better Naming Convention replaces the single-character prefix. This naming convention is called the Hungary markup-it uses a three-character prefix to represent the data type and control type. • How to intuitively match the data type assigned to the variable:
D i m strNameAs String
• Three characters can be used to make the prefix logical and intuitive. Next, let's look at the previous Code statement:
TotalDue = LineItemTotal – Discount + TotalTax
Statement of equal price
txtTotalDue.Text = curLineItemTotal –txtDiscount.Text + curTotalTaxcurTotalDue = curLineItemTotal – curDiscount + curTotalTaxtxtTotalDue.Text = txtLineItemTotal.Text – txtDiscount.Text + _txtTotalTax.Text
• These prefixes make code statements easier to understand. • You only need to read the statements to know what happened,
General rules
Name should be able to identify the characteristics of things.
All names use English words instead of pinyin.
Do not use abbreviations unless they are well known.
The name may consist of two or three words, but should not contain more than three words. The name must be 3 to 30 letters.
In a name, multiple words are separated by the first letter (lower case) in upper case. For example, issuperuser.
Try to use a prefix instead of a suffix for the name.
The words in the name should use nouns as much as possible. If there are verbs, they should be placed behind them as much as possible. For example, functionuserdelete (instead of functiondeleteuser ).
In specific task development, if there are specific naming conventions, the corresponding software development plan should be clearly defined and reported to the Quality Management Department audit team.
Naming Convention variable)Pascal: case-the first letter of all words is in upper case, and the other letters are in lower case.
CAMEL: In case-except the first word, the first letter and other letters of all words are in upper case.
In the. NET coding specification, all variables are named in the camel format, instead of using the Data Type and M _ as the prefix.
Use meaningful and descriptive words to name variables
-Do not use the abbreviation. Use name, address, salary, and so on to replace Nam, ADDR, Sal
-Do not use single-letter variables such as I, n, and X. Use index,
Temp, etc.
Variable exceptions used for loop iteration:
for ( int i = 0; i < count; i++ ){ ...}
-The variable name does not contain underscores (_).
-The namespace must be named in standard mode.
In the program, it is necessary to clarify the scope of the variable, so it stipulates:
-Global variable name: the prefix 'G' is added to indicate (global ).
Example: String gstrname; int giid;
-Local variable name: variable type + variable name.
Example: String strname; int IID;
-Static variable name: the prefix 's' is added to indicate (static)
For example: static string sstrname; static int siid;
-Read-Only variable name: prefix 'ro' (readonly)
Example: readonly string rostrname; readonly int roiid;
Note:
Naming exception variables during exception capture is the same as exp without conflict;
If there is a conflict, you can use the "exp" + flag name, for example, expsql.
Try{ //your code try{//code}catch(Exceptionexp){//your code}}catch(ExceptionexpSql){ //your code}
Supplement: if you do not need to handle exceptions, you do not need to define an exception instance.
Example:
try{ //your code}catch( Exception exp){}
Ø since most names are constructed by concatenating several words, use a mix of upper and lower cases to simplify their reading. The first letter of each word is in uppercase.
Even for variables that may only have a short lifetime in several code lines, meaningful names are still used. Only use single-letter variable names for short-cycle indexes, such as I or
J.
Use complementary pairs in variable names, such as min/max, begin/end, and
Open/close.
Do not use the original numeric or string, such as for (I = 1; I <= 7; I ++ ). Instead, use the naming constant, as shown in figure
For (I = 1; I <= num_days_in_week; I ++) for maintenance and understanding.
The constant name must also have a certain meaning, in the formatN/OrNoun_verb. The constant names are in uppercase and separated by underscores.
Example:
Private constbool WEB_ENABLEPAGECACHE_DEFAULT =true;privateconstint WEB_PAGECACHEEXPIRESINSECONDS_DEFAULT = 3600;private constbool WEB_ENABLESSL_DEFAULT =false;
Note:
Variable names and constant names can contain a maximum of 255 characters. However, names with more than 25 to 30 characters are clumsy. In addition, to get a meaningful name, clearly express the use of variables or constants, 25 or
30 characters should be enough.
Class) Name
A) The name should be able to identify the characteristics of things.
B) Try not to use abbreviations unless they are well known.
C) A name may consist of two or three words, but generally should not contain more than three words.
D. Use Pascal to write the first letter of all words in uppercase. For example, issuperuser, which contains IDs, all in uppercase, such as customerid.
E) Use a noun or a noun phrase to name a class.
F)
Use less abbreviations.
G) do not use the underscore (_).
Example: Public classfilestreampublic classbuttonpublic classstring
Interface) Name
The same as the class naming convention, the only difference is that the interface is prefixed with "I" before the name.
Example: interface idbcommand; interface iButton;
Method) Name
A) the class naming rules are the same.
B) The method name must be able to see what it does. Do not use a name that may cause misunderstanding. If the name is clear, you do not need to use documents to explain the functions of the method.
(Namespace) NameSame as the class naming rules.