The general naming rules for computer variables are:
The first letter of the variable name must be a letter (A-Z-a-to), an underscore (_), or a dollar sign ($), and all variables in PHP programming must begin with $. (Some compilers already support Chinese variable names) variable names can only be alphabetic (A-Z-A-Z), Numbers (0-9), underscores (_), and cannot contain spaces, numbers cannot be placed in the first variable name. Variable names cannot use reserved words in the programming language. For example, you cannot use true,false,while,case,break reserved words in JavaScript, and so on. Use short enough and long enough names within each code range: for example, a loop calculator can be used with a single character, such as I; condition and loop variable with one word, method name 1-2 words, class name 2-3 words, global variable 3-4 words make a variable specify some special name, do not use such as "value" , "equals", "data" variable name variable names to use a meaningful name, through the variable name can probably reflect its specific purpose do not prefix the variable name, such as O_, Obj_, m_, such as obey the company naming norms, in naming the use of consistent practice, For example: txtUserName, Lblusername, Cmbschooltype, ... Do not use inconsistent lowercase and uppercase mixed names, such as: UserName, UserName, user_name, M_username, UserName, ..., unless it is significantly less readable to obey the specifications of the programming language itself. Do not reuse the same variable names in different contexts of the same class do not use the same variable for different purposes in a single method do not apply non-ASCII character variables, such as Chinese variables do not use too long variable names, such as 50 characters, which is difficult to read, And probably beyond some compiler limits to determine and persist in the use of a fixed natural language naming, such as do not use Pinyin and English mixed naming method name to use a meaningful name, can be used by the method name to understand the purpose of the method, and the method name to start with a verb, For example, the Createpasswordhash method name conforms to the company's uniform specifications, for example: Gettxtusername (), Getlblusername (), isstudentapproved () Method name to follow the parameter name of the programming language specification method to use a meaningful name
There are three classic naming conventions
- Hungarian nomenclature. The naming method is preceded by a number of characters representing the data type in front of each variable name. The basic principle is: variable name = attribute + Type + object description. If I is an int, all variables starting with I represent the int type. s represents a string, and all variables that start with s represent a string type variable.
- Camel name law. As its name implies, it means mixing uppercase and lowercase letters to form the names of variables and functions. The hump naming method is similar to the Pascal nomenclature, except that the initials are lowercase, such as username. Because it looks like a hump, and hence the name.
- Pascal's nomenclature is the Pascal nomenclature method. It is capitalized in the first letter, such as username, and is commonly used in variable naming of classes
One: Hungarian nomenclature
The Hungarian nomenclature is a naming convention when programming. The basic principle is: variable name = attribute + Type + object description, where each object name requires a clear meaning, you can take the full name of the object or part of the name. Based on principles that are easy to remember and easy to understand. It is very important to ensure the consistency of the name.
For example, if the name of the form is a form, then the Hungarian nomenclature can be abbreviated as FRM, then the full name of the variable should be frmswitchboard when the form variable is named switchboard. This makes it easy to see from the variable name that switchboard is a form, and similarly, if the variable type is a label, it should be named Lblswitchboard. It can be seen that the Hungarian nomenclature is very easy to remember, and makes the variable name very clear and understandable, thus enhancing the readability of the code, convenient for each programmer to communicate with each other code.
It is said that this nomenclature was invented by a Hungarian programmer named Charles Simonyi, who later spent several years at Microsoft, and the nomenclature spread through Microsoft's various products and documentation. Most programmers use this nomenclature, no matter what software they use to develop them. The starting point of this nomenclature is to combine variable names by: The Order of attribute + Type + object description, so that programmers have a visual understanding of the types of variables and other properties when they make variables, the following is the HN variable naming convention. Properties section: G_ global variable C_ constant M_ C + + class member variable s_ static variable type part: Array A pointer p function fn Invalid v handle h long integer l Boolean b float type (sometimes referred to as file) F double word DW string sz short integer n double precision floating point D count C (usually with CN T) character ch (usually with C) integer I (usually with n) byte by word w real Type R unsigned u description section: Max Max min min Initialize init temp variable T (or temp) source object src Destination Object desthwnd:h is a type description, representing a handle, WND is a variable An object description that represents a window, so the HWND represents a window handle; PFNEATAPPLE:PFN is a type description, a pointer to a function, Eatapple is a variable object description, so it represents a function pointer variable that points to the Eatapple function. G_cch:g_ is a property description that represents a global variable, and C and CH are count types and character types, together representing the variable type, where the object description is omitted, so it represents a global variable that counts the characters. Above is the general rule of the HN nomenclature.two, hump naming methodCamel-style nomenclature (camel-case), also known as the Hump nomenclature, is a set of naming conventions (conventions) when writing a computer program. As its name CamelCase implies, it means mixing uppercase and lowercase letters to form the names of variables and functions. Programmers in order to their own code can be easier to communicate between peers, so more than a unified readability of the better way to name. Some programmers like to have all lowercase, some programmers like to underline, so if you want to write a variable of my name, they often have myname, my_name, MyName, or myname. Such a naming convention is not suitable for all programmers to read, and the use of the Camel name method to express, can increase the readability of the program. For example, the following is the same function named by camel-named and underscore: Printemployeepaychecks (); Print_employee_paychecks (); The first function name uses camel-named notation Each logical breakpoint in the function name is marked with an uppercase letter, and the second function name uses the underscore method----each logical breakpoint in the function name is marked with an underscore. Camel-like nomenclature is used quite a lot in environments such as many new libraries and Microsoft Windows. On the other hand, the underscore is popular after the advent of C, in many of the old Programs and UNIX environment, it is very common use. Camel-style nomenclature is when a variable name or function name is a unique identifier consisting of one or more words, the first word starts with a lowercase letter, the first letter of the second word is capitalized, or the first letter of each word is in uppercase letters, for example: MyFirstName, Mylastname, Such variable names look like camel peaks, so they get their name. The term camel-named (camel-case) is a camel that comes from the widely used case-and-write format in Perl, and the cover picture of Programming Perl (O ' Reilly), a bestseller of Larry Wall. The naming rules of camel-like nomenclature can be regarded as a convention, and there is no absolute and mandatory, in order to increase recognition and readability. Small Hump method is usually identified by small hump method. The Hump method means that in addition to the first word, the first letter of the word is capitalized. For example, int mystudentcount; variable Mystudentcount The first word is all lowercase and the first letter of the following word is capitalized.Large Hump MethodCompared to the small hump method, the big hump method capitalized the first letter of the initial word. Commonly used for class names, function names, properties, namespaces. For example, public class Databaseuser;
three, Pascal's name LawPascal's nomenclature is used to increase the readability of variables and functions when the variable name and function name are unique identifiers that are linked together by two or more than two words. The word is not separated by a space break or a connection number (-), the bottom line (_), the first letter is capitalized, and the first letter of the subsequent word is also capitalized, for example: FirstName, LastName. The initials of each word are named in uppercase letters, known as the Pascal Nomenclature, and are derived from the naming conventions of the Pascal language, also known as the "big hump-type nomenclature" (Upper Camel case), a subset of camel-cased. Pascal's nomenclature is to capitalize the initials at the time of naming, such as: program code public void DisplayInfo (), String UserName, and both using Pascal's nomenclature. In C #, the name of Pascal and the name of the camel are mostly. In coding conventions in C #, you should use the Pascal nomenclature when naming common member variables (public), protected member variables (protect), or internal member variables (internal), such as score, name, and status as valid member variable names The private member variable must be named by the camel name and begin with an underscore. 1 "Pascal nomenclature" can be regarded as a naming convention, and there is no absolute and mandatory, in order to increase recognition and readability. Once you have chosen or set a naming convention, you should keep the formatting consistent when you write the program.
Computer language variable naming rules