Stylecop (C # code standardization analysis tool)-2. Introduction to common rules (1)

Source: Internet
Author: User

Send cainiao

For a cainiao programmer like me, the tasks assigned to me by the leader can be completed on time. How can I handle the code elegantly and readability! Even if there is one, we have this level ,...! No one cares about him! As long as I understand it! Because I didn't pay attention to it at ordinary times, the boss asked me to maintain the projects I had previously done. The results were completely dumpfounded, and I had no idea at all! It cannot be my work! The code is messy, and the defined statement can only guess its intention. It is clear that you only need to modify a small place, but you need to understand the entire program from start to end, delaying the time! Recall that when I first wrote the code, I had to ignore the code standardization in order to accomplish the task as early as possible. Now I can't regret it! Therefore, we decided to develop a good encoding style from now on. Although the encoding speed will be reduced, it is worthwhile in the long run! However, my skills are limited. Is there any tool with similar functions! It is a matter of success.

 

Experience

At the beginning, to be honest, this guy was a little abnormal. A common program gave dozens of warnings! And I don't quite understand it! If you feel that you have a good reason for doing so, you do not have to follow the stylecop rules. The following describes some of the rules that I often violate in stylecop.

 

Common rules

Basic

Invalid spacing around the comma: ',' should be followed by a space (a few do not matter)

Invalid spacing around the opening curly bracket: '{' must be followed by a space (only one, or else the 'code contains multiple spaces in a row. Only one space is needed ')

Invalid spacing around the closing curly bracket: '}' must be preceded by a space (same as above)

All using directives must be placed inside of the namespace: The using command must be moved to the namespace.

For example:

Using system;
Using system. text;

Namespace stylecopdemo
{
Public class person
{
}
}
// It should be
Namespace stylecopdemo
{
Using system;
Using system. text;

Public class person
{
}
}

I am also a little dizzy! But there is an article!

Adjacent elements must be separated by a blank line: Elements adjacent to each other must be separated by space lines, for example, between the using namespace and namespace.

An opening curly bracket must not be followed by a blank line: '{' The following row cannot be empty

A closing curly bracket must not be preceded by a blank line: '}' the above row cannot be empty

Using directives must be sorted alphabetically by the namespaces: using statements must be sorted alphabetically (in order)

The code must not contain multiple blank lines in a row: multiple lines of empty lines are not allowed in the code.

Error:

Correct:

Document specifications:

The class must have a documentation header: the defined class must contain the file header flag '//' and the content should be described with spaces (a little abnormal ), otherwise, the message 'the documentation text within the summary tag does not contain any whitespace between words, indicating that it most likely does not follow a proper grammatical structure required for documentation text' may be reported, for example:

/// <Summary>
/// Basic user information
/// </Summary>
Public class person
{
}

The file has no header, the header XML is invalid, or the header is not located at the top of the file:

You need to add the following at the beginning of the file:

//-------------------------------------------------------
// <copyright file="Person.cs" company="MyCompany">
// Copyright MyCompany. All rights reserved.
// </copyright>
//-------------------------------------------------------

Note the indent format.

A c # document may only contain a single class at the root level unless all of the classes are partial and are of the same type: [guess] C # documents generally only contain descriptions of one class. Unless these classes are of the partial type or the same type. This error is generally caused by multiple classes in a *. CS file.

Function Specification:

The method must have an access modifier: The method requires an access modifier (Public Private protected ..)

The method must have a documentation header: The method must start.

'//' Specification requirements:

The content in the tag cannot be blank. The content should be separated by spaces. Otherwise, 'the documentation text within the summary tag does not contain any whitespace between words will be reported, indicating that it most likely does not follow a proper grammatical structure required for documentation text ';

Instance:

 

Field specifications:

The field must have an access modifier: The field must have a modifier

Variable names must start with a lower-case letter: The field name must start with a lower case

The field must have a documentation header: The field must have a document description, starting //

Some programmers like to name fields starting with _, but stylecop is not recommended. (Field Names must not start with an underscore)

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.