Differences between the. NET naming conventions

Source: Internet
Author: User
It is a good practice for the specification to use annotation statements in your code and the uniform naming rules that are used below to make your code more generic. I have been working at Microsoft for nearly five years. I don't know if you're like me. There are many problems with integrating and removing bugs----because developers do not follow uniform naming rules and write comment statements.
In previous versions of Visual Studio, Microsoft had suggested using the Hungarian nomenclature to write code and encouraged the development of the same rules for writing code in a unified format. For example:


Variable
Named

CString
Szstring

Char
Cmychar

char*
Pmychar

Long
Lmyvariable

Lpcstr
LpStr

Int
Nmynumber


In the recently released. NET and its programming language, Microsoft has changed this rule. If you have used Delphi programming, you will see that the new rule is somewhat similar to that of Delphi (Pascal).

To avoid code collisions (which would also make your code more generic), it's a good programming practice to use naming laws. Well, here are some common rules.

Named variables, methods, and properties

The first letter of the name of the variable, method, and property should be capitalized, and the name should be able to express their purpose (or meaning).

Variable
. NET naming
Hungarian name
Describe

Cstring
EmployeeName
SzName
Name of an employee.

Int
Attendancecounter
Ncounter
A counter of type long.

Long
Numberofbytes
Lbytes
A Long type variable stores bytes.


Sometimes we are accustomed to using the underscore "_" in the definition, for example: Add_data (int a, int b). According to the new naming law, this is not a good programming habit, although it is not wrong. You should change the definition add_data to AddData. This is not a Microsoft standard and you don't have to follow these rules. However, in some places you will understand the rationality of these laws below.

Personally, I prefer Hungarian nomenclature. Of course, variables use the same rule. If you remember the Hungarian nomenclature, a Boolean variable is defined with "B", for example:

BOOL bflag = TRUE;


The new law does not recommend the use of flag and "B":

bool Isfilefound = true;

You can browse MSDN for more information on the new rules.


Naming components and collections (assemblies)

In order to avoid code conflicts, naming your library (called Assembly in. NET) is a good programming practice. Assuming you are Mindcracker company, you are developing a library to expand the C # database class, naming it MindCracker.DatabaseAssembly.ADOSet Much better than the MyAssembly.Database.ADOSet.

Then assume that your library has a method that reads data from a table and returns a DataSet. If you name the DataSet Return_data (), change to a DataSet Returndata () ok.

You should also follow a uniform word order in the naming. For example, you have two tables, employee and Manager respectively, and you also define a method for them to insert a record into the table, so the method name Appendemployee and Appendmanager are more than Appendemployee and Managerappend better.


Cycle

Loops or other block structures should align the left and right braces, like this:

for (i = 0; i < i++)
{
...
}

Or

for (i = 0; i < i++) {
...
}


I prefer the two methods because it is easy to understand a block structure and nested block structure.


Hungarian nomenclature (efoxxx attached)

Hungarian nomenclature was invented by a Hungarian programmer, and he worked for many years at Microsoft. This nomenclature is passed through Microsoft's various products and documents. Most experienced programmers, no matter what language they use, are more or less using it

The basic principles of this nomenclature are:

Variable name = attribute + Type + Object description

That is, a variable name is composed of three parts of information, so that the programmer can easily understand the type of variable, use, and easy to remember.

Below are some of the recommended rule examples that you can choose from, or you can modify them to suit your personal preferences.

⑴ Properties section:

Global variables: g_

Constants: C_

Class member Variable: m_

⑵ Type section:

Pointer: P

Handle: H

Boolean type: b

Floating-point type: F

No sign: U

⑶ Description section:

Initialization: Init

Temp variable: TMP

Destination object: Dst

Source object: SRC

window: WND


The following examples illustrate:

Hwnd:h represents a handle, wnd represents a window, and is combined as a "window handle".

M_BFLAG:M represents a member variable, and B represents a Boolean, which together is: "A member variable of a class, a Boolean, is a status flag."

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.