C # naming convention

1, naming conventions

Pascal and Camel naming conventions

Programming naming Pascal and Camel are mainly two kinds (Pascal: first letter of each word capitalized, for example, ProductType; Camel: the first word of the first letter lowercase, uppercase first letter of the rest of the word, such as productType)

Here are some commonly used C # and members of their recommended naming method:

Identifier

rule

Description and Examples

Class class

Pascal

Application

Enumeration type enum

Pascal

Remember, the name is Pascal, do not contain Enum, otherwise FXCop throws Issue

Principal delegate

Pascal

To Pascal named, not in any particular string is different from the class name, function name

Constant const

ALL CAPS

All uppercase, separated by an underscore between words

Interface interface

Pascal

IDisposable Note: I always prefix followed by the pair named Pascal

Method function

Pascal

ToString

Namespace namespace

Pascal

. To separate, when each qualifier Pascal are named, such as:

using ExcelQuicker.Framework

parameter

Camel

The first letter lowercase

Local variables

Camel

Type identifier can also be added, such as for System.String type, declare a variable begins with str, string strSQL = string.Empty;

Data members

Camel

Beginning with m + Pascal naming rules, such as mProductType (m meaning member)

Attributes

Pascal

1.1, a local variable named

When naming a local variable primitive, using Camel naming,

For example: int type = 0;

double count = 0;

For the definition of the string type, usually str + Pascal prefix naming manner,

For example, string strSql = ""; // This is a typical way of naming SQL statement string.

As for the addition of the type of object definitions, the usual practice is to use the prefix + Pascal named obj way to tell us this variable is an object. Or you can directly use the class name of Camel naming rules.

比如:Application objApplication = new Application();

            Application application = new Application();

1.2, parameter name

Camel naming convention, the first letter lowercase

1.3, class data members / property name

Data members named Camel naming, naming and attribute to Pascal. Typically, if a data member and attribute pairs, then the difference between naming and attribute data members that only the first letter of the variable name is lowercase or uppercase.

such as

class Appcalition

{

        private ArrayList worksheetCollection = new ArrayList();

        public ArrayList WorksheetCollection

        {

            get

            {

                return this.worksheetCollection;

            }

        }

}

In addition, when the data members of the class / method calls, you should add this qualifier, this is blue, but also help us distinguish between local variables, parameters or static variables, and is conducive to FXCop detection distinction editing environment. (If FxCop scanning and detection tags)

1.4, Namespaces

In each defined between the dot line with Pascal string format

1.5, commissioned by the abbreviation

I often entrusted to Pascal naming names, and added later named EventHandler

Such as public delegate void MouseEventHandler (object sender, MouseEventArgs e); // for processing the mouse-related events or delegate

For custom commissioned its first proposal still use parameters object sender, sender on behalf of the trigger source object this time or delegate. The second parameter is inherited from EventArgs class, and implement your own business logic in a derived class.

1.6, custom exception class

Ending custom exception class Exception, and class names can clearly describe the reason for the exception. For example NotFoundFileException, describe a certain entity (file, memory area, etc.) can not be found.

1.7 Enumeration

Enumeration named Pascal naming, without the addition of Enum in the enumeration, enumeration name can clearly indicate the purpose of the enumeration.

1.8, named constants

ALL CAPS, between words and underscore intervals, such as public const int LOCK_SECONDS = 3000; Although the recommended Pascal in constant MSDN named, but from naming C ++ followed the point of view, the constant in all caps more clearly represents a constant and the difference between ordinary variables.

1.9, abbreviated name

In general, not recommended acronym name, do not worry about long variable names, meaning long variable names can make variables more clear, in fact, negative effects from long variable names three as Ctrl + C and Ctrl + V plus VS IntelliSense, with the negative chasing very small. Variable naming principle is do our best to let other people see our variables / functions / etc ... the first time, probably can guess what it is doing.

For example: int productTypeCount = 0; // we will know it is a record of a variable number of products in the first time

As for the bad naming: int prodTypeCount = 0; // it is productTypeCount shorthand, some of us may know prod is the abbreviation for the product, but no one can guarantee that all the people know it. Personally, I think: the best code which is itself a comment. As the leading programmer. Not only realize the function, but to make our code more beautiful, others have the ability to make future maintenance or expansion. As the current business system, its entry level threshold has been greatly reduced, to achieve functional requirements have been no difficulty, but the difference between the master and the rookie is that the master code is easy to understand, the entire coding process, not only can considering the performance, but also consider the code readability and maintainability.

1.10, database name

Column in the database table names are named Pascal recommended naming, try not to use abbreviations. Of course, the negative impact of the use of long field names, table names, writing may make SQL statements. I recommend you can use some ORM, performance ORM certainly not better than directly write SQL, but if you do business system, is more important is how long the system can be delivered to users, ORM not only a lot of development time can be shortened, and the maintenance of late there are many convenient than the direct write SQL.

2 comments norms

2.1, the file header comments

Annotate the code in the header file, the benefits of doing so is that we can do to change the tracking code file. In the first part of the code marked a founder, founding time, people modify, modify the function of time, the code, which is essential in the development of the team, they can make later maintenance / modification companions when they encounter problems, the first time he should know who to go to for help, and know that this document has gone through how many iterations, through the development and modification of how many programmers.

sample:

/********************************************************************************

** Author: Eunge

** Founding time: 2004-6-8

** Modified By: Lucy

** Modified: 2004-12-9

** Modified By: Lucy

** Modified: 2005-01-29

** Description:

** mainly used for data entry product information, ...

*********************************************************************************/

2.2, functions, attributes, etc. Notes

Use /// three slash comment, this comment is based on XML, XML can be derived not only make help files, and in all functions, properties, etc. of use, the editing environment automatically bring out the Notes to facilitate your development. With protected, protected Internal, public statements have suggested that the comments of this naming method.

E.g:

/// <summary>

/// remove a product from the ERP system information classes

/// </summary>

class ProductTypeCollector

{

       …

}

2.3, logical point Notes

We believe that adding a strong logical place to comment, explain the logic of this program is how to facilitate the later understanding of our own and other people's understanding, and this can also exclude certain extent BUG. Stated in the comments of our logical thinking, control procedures, to determine whether the procedures in line with our original intention, if not, then we should think carefully about Yao modification program is a comment or a ...

3, typesetting

I typographic principles and recommendations:

1, each row comprises at least one row statement if the statement is too long (more than one screen), the statement is broken into two lines;

2, the similar content together, such as data members, properties, methods, events, and appropriate use # region ... # endregion, I like the machine-generated code is placed inside a #region, such as in the preparation when ASP.NET program, the corresponding control defines automatically generated, I used #region automatic generated Web Components ... #endregion them framed

3, the use of space ,

Front (1) of spaces binary operators (+, =, &&, etc.), index = index + 1;

(2) The unary operator spaces (, +, -, etc.!), Index ++;

(3) commas, semicolons spaces behind only

4, blank lines, in the period between the function code, or functions, attributes insert blank lines, it will be very intuitive.

In Visual Studio 2005, a fact already with this function code format, the shortcut key is Ctrl + K -> Ctrl + D.

4, interface controls named

My suggestion is to use the default control name as a prefix, the prefix name all lower case, so that the benefits do not have a unified naming unknown controls to worry about, such as the label for the Label control, some people use the abbreviation lbl, some people use lab, there people with a lb. This is actually still avoid abbreviations, sometimes still make the name becomes tedious, but more able to reflect the significance of naming variables, and each developer can perform better, because they do not need to memorizing each variable abbreviation.

protected System.Web.UI.WebControls.Button buttonQuery;

protected System.Web.UI.WebControls.DropDownList dropdownlistProductType;

protected System.Web.UI.WebControls.TextBox textboxManufactureDate;

5, code readability some suggestions

Priority (1) Note that operator, we should try to use the explicit expression of the sequence of operations brackets, avoid using the default priority, for us as well as the maintenance man bring trouble

(2) avoid the use of digital difficult to understand, identify a meaningful alternative to (and enumeration constants)

such as:

if(productType == 0)

       …

else if (productType == 1)

       …

(Not recommended)

if(productType == ProductType.CD)

       …

else if (productType == ProductType.DVD)

       …

(Recommended Use)

(3) to make use of the interface layer, exception handling try statements, system-level error will not directly exposed to the user, but more error messages should be logged to the system thrown LOG log file to tell the user-friendly tips information

In Visual Studio 2005 inside, there are code layout formatting features, quite useful. In fact, regulation code is to make the system as a whole consistent coding style, so that post-maintenance personnel to more quickly understand code and maintenance. I think the code specifications its necessity, but can not because the norms and standards, from the development, the development is faster in order to make the system stable, and stable system is designed to bring benefit to the company. Developers, project managers should be more from the perspective of project management out, while standing in the company, the customer's point of view the problem, not because of the code and the code.

Reproduced in: https: //my.oschina.net/cookblack/blog/621399

Guess you like

Origin blog.csdn.net/weixin_34348111/article/details/92046063