日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

StyleCop(C#代码规范分析工具)---2.常用规则介绍(一)

發(fā)布時(shí)間:2025/7/14 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 StyleCop(C#代码规范分析工具)---2.常用规则介绍(一) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

寄菜鳥

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

?

體會(huì)

  剛開始時(shí),說實(shí)話感覺這個(gè)家伙有點(diǎn)變態(tài),普通的一個(gè)程序,就幾十個(gè)警告!而且有些不太理解!如果覺得自己這樣做有充分的理由,也不必遵循StyleCop的規(guī)則。下面開始介紹StyleCop的一些我常常違反的一些規(guī)則。

?

常用規(guī)則

基礎(chǔ)

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

Invalid spacing around the opening curly bracket:’{‘前后需要加空格(僅僅一個(gè),要不然就報(bào)’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指令需要移到命名空間內(nèi)

例如:

using System;
using System.Text;

Namespace StyleCopDemo
{
Public class Person
{
}
}
//應(yīng)該為
Namespace StyleCopDemo
{
using System;
using System.Text;

Public Class Person
{
}
}

這個(gè)我也有點(diǎn)暈了!不過有一篇文章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:代碼中不允許一行中有多行空行。

錯(cuò)誤:

正確為:

文檔規(guī)范:

The class must have a documentation header:定義的類必須含有文件頭標(biāo)志’///’而且其中的內(nèi)容介紹最好是帶有空格的(有點(diǎn)變態(tài)),不然可能會(huì)報(bào)’ 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>
//-------------------------------------------------------

注意縮進(jìn)格式。

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#文檔一般只能包含對(duì)一個(gè)類的描述。除非這些類是partial類型或者是同類型的。這種錯(cuò)誤一般是在一個(gè)*.cs文件中有多個(gè)不同類型的類。

函數(shù)規(guī)范:

The method must have an access modifier:方法需要訪問修飾符(public private protected..)

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

‘///’規(guī)范要求:

標(biāo)簽內(nèi)的內(nèi)容不允許為空。內(nèi)容最好是用空格隔開否則會(huì)報(bào)‘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’;

實(shí)例:

?

字段規(guī)范:

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)

轉(zhuǎn)載于:https://www.cnblogs.com/usharei/archive/2012/03/31/2426641.html

總結(jié)

以上是生活随笔為你收集整理的StyleCop(C#代码规范分析工具)---2.常用规则介绍(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。