StyleCop(C#代碼規範分析工具)—2.常用規則介紹(一)

來源:互聯網
上載者:User

寄菜鳥

  對於像我這樣還是菜鳥級的程式員來說,Leader分配給我的工作,只要按時做完就OK,哪有時間去理代碼的優雅,可讀性!就算有,就咱這個水平,。。。!別人看不懂管他呢!只要我看得懂不就行了!由於平時沒有注重,老大讓我去維護自己以前做過的項目,結果完全傻眼了,咋一點印象都沒有了呢!這不可能是我做的吧!代碼混亂不堪,定義的語句只能去猜它的本意,明明只要修改一個小地方,卻要從頭到尾去瞭解整個程式,耽誤時間!回想起當初寫代碼時為了追求能儘早的完成任務,忽略了代碼的規範性,現在真是追悔莫及!所以決定從現在開始養成一個良好的編碼風格,雖然編碼的速度會有所降低,但是從長遠出發,是很值得的!但是自己水平有限,有沒有類似功能的工具呢!功夫不負有心人,它就是StyleCop.

 

體會

  剛開始時,說實話感覺這個傢伙有點變態,普通的一個程式,就幾十個警告!而且有些不太理解!如果覺得自己這樣做有充分的理由,也不必遵循StyleCop的規則。下面開始介紹StyleCop的一些我常常違反的一些規則。

 

常用規則

基礎

Invalid spacing around the comma :’,’後面需要加空格(幾個無所謂)

Invalid spacing around the opening curly bracket:’{‘前後面需要加空格(僅僅一個,要不然就報’The code contains multiple spaces in a row. Only one space is needed’)

Invalid spacing around the closing curly bracket: ‘}’前面需要加空格(同上)

All using directives must be placed inside of the namespace: using指令需要移到命名空間內

例如:

using System;
using System.Text;

Namespace StyleCopDemo
{
Public class Person
{
}
}
//應該為
Namespace StyleCopDemo
{
using System;
using System.Text;

Public Class Person
{
}
}

這個我也有點暈了!不過有一篇文章http://blogs.msdn.com/b/abhinaba/archive/2006/08/21/709051.aspx,介紹了這樣做的好處!

Adjacent elements must be separated by a blank line:緊鄰的元素之間必須用空格行隔開,例如using命名空間和namespace之間。

An opening curly bracket must not be followed by a blank line: ‘{‘下面一行不允許是空行

A closing curly bracket must not be preceded by a blank line: ‘}’上面一行不允許是空行

Using directives must be sorted alphabetically by the namespaces:using語句必須要按照字母排序(順序)

The code must not contain multiple blank lines in a row:代碼中不允許一行中有多行空行。

錯誤:

正確為:

文檔規範:

The class must have a documentation header:定義的類必須含有檔案頭標誌’///’而且其中的內容介紹最好是帶有空格的(有點變態),不然可能會報’ 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’,例如:

    /// <summary>
/// 使用者類 使用者基本資料
/// </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:

需要在檔案開頭上加:

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

注意縮排格式。

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:【猜】C#文檔一般只能包含對一個類的描述。除非這些類是partial類型或者是同類型的。這種錯誤一般是在一個*.cs檔案中有多個不同類型的類。

函數規範:

The method must have an access modifier:方法需要存取修飾詞(public private protected..)

The method must have a documentation header:方法必須要文檔說明就是以‘///’開頭的。

‘///’規範要求:

標籤內的內容不允許為空白。內容最好是用空格隔開否則會報‘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’;

執行個體:

 

欄位規範:

The field must have an access modifier:欄位必須要有修飾符

Variable names must start with a lower-case letter:欄位的名字必須是小寫開頭的

The field must have a documentation header:欄位必須要有文檔說明,以///開頭的

有些程式員喜歡以_開頭來命名欄位,但是StyleCop是不推薦的。(Field names must not start with an underscore)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.