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


寄菜鳥

  對於像我這樣還是菜鳥級的程序員來說,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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM