前言
此處只是整理并記錄下.Net開發規范以便加深編碼規范,自己編碼一直都是隨心所欲,特別是在Java和C#之間來回切換,導致命名也亂七八糟完全沒有規范可言,一個良好的編程規范可以提升程式員的開發效率、提高程式的易讀性、提高代碼的可維護性等,本文分兩部分:通用規范、.Net開發規范,
通用編程規范
1. 明確性和一致性
一定要要確保代碼的明確性、易讀性盡量保證清晰簡潔,如函式的命名及該有的注釋必須要有,讓別人一看函式就知道該函式是干什么用的,

2. 庫的使用
洗掉不需要的庫參考,

3. 盡量少用全域
盡量少用全域變數,如果要用只讀不修改影響不大,但涉及變數的修改就一定要檢查變數更改對其它地方的影響,在VS中用“查找所有參考”核查所有用到的代碼,

4. 變數申明和初始化
- 在變數申明時進行初始化;
- 一定在最小作用域內申明它,一般申明于作用域頂端;
- 變數初始化置于同一行,推薦每行只包含一句申明;

5. 函式申明和呼叫
- 函式名稱,回傳值,引數原則置于同一行;
- 過多引數一個引數一行(推薦);
- 多個輸入引數時建議對輸入引數進行排序;
- 當存在多個重構函式時,擴展引數因往后追加;
- 當存在輸出引數時候,輸出引數放末尾或開頭;
1 static void Main(string[] args) 2 { 3 string logMsg = null; 4 string erroMsg = null; 5 string userName = "zhangsan"; 6 string userPwd = "123456"; 7 8 //如果引數較少,可放置一行呼叫 9 string result = DoSomeFunctionCall(userName, userPwd, false, out logMsg, out erroMsg); 10 11 //如果引數較多情況下,可用如下方式進行呼叫 12 string result2 = DoSomeFunctionCall( 13 userName, 14 userPwd, 15 false, 16 out logMsg, 17 out erroMsg); 18 19 Console.WriteLine($"result={result}, result2={result2}"); 20 } 21 22 /// <summary> 23 /// 示例函式描述資訊 24 /// </summary> 25 /// <param name="userName">用戶名</param> 26 /// <param name="userPwd">用戶密碼</param> 27 /// <param name="isRememberPwd">是否記住密碼</param> 28 /// <param name="logMsg">日志資訊</param> 29 /// <param name="errorMsg">錯誤資訊</param> 30 private static string DoSomeFunctionCall( 31 string userName, 32 string userPwd, 33 bool isRememberPwd, 34 out string logMsg, 35 out string errorMsg) { 36 37 logMsg = null; 38 errorMsg = null; 39 40 //Do Something 41 42 return "some result"; 43 }
6. 列舉
將代表某些值集合的強型別引數,屬性和回傳值宣告為列舉型別,
7. 空格
空行:適度增加空行來增加代碼的可讀性;
空格:可使用VS回車自動格式化了;
8. 注釋
注釋最好簡潔明了,一定不要過多的冗余注釋,一般注釋包括:檔案頭注釋、函式注釋、多行注釋、單行注釋、行內注釋、代碼快注釋,
/* 檔案頭注釋:描述整個類的主要用途,如此類主要用于測驗使用, */ using System; using System.ServiceProcess; using System.Threading; namespace StartServices { class Program { /// <summary> /// 函式注釋:控制臺函式入口 /// </summary> /// <param name="args"></param> static void Main(string[] args) { string logMsg = null; string erroMsg = null; string userName = "zhangsan"; string userPwd = "123456"; //單行注釋:如果引數較少,可放置一行呼叫 string result = DoSomeFunctionCall(userName, userPwd, false, out logMsg, out erroMsg); /* * 多行注釋: * 如果引數較多情況下,可用如下方式進行呼叫 */ string result2 = DoSomeFunctionCall( userName, //行內注釋:用戶名 userPwd, //密碼 false, //是否記住密碼 out logMsg, //日志資訊輸出 out erroMsg); //錯誤資訊輸出 #region 代碼塊注釋:控制臺資料輸出 Console.WriteLine($"result={result}, result2={result2}"); Console.WriteLine($"result={result}, result2={result2}"); Console.WriteLine($"result={result}, result2={result2}"); Console.WriteLine($"result={result}, result2={result2}"); Console.WriteLine($"result={result}, result2={result2}"); Console.WriteLine($"result={result}, result2={result2}"); #endregion } /// <summary> /// 示例函式描述資訊 /// </summary> /// <param name="userName">用戶名</param> /// <param name="userPwd">用戶密碼</param> /// <param name="isRememberPwd">是否記住密碼</param> /// <param name="logMsg">日志資訊</param> /// <param name="errorMsg">錯誤資訊</param> private static string DoSomeFunctionCall( string userName, string userPwd, bool isRememberPwd, out string logMsg, out string errorMsg) { logMsg = null; errorMsg = null; //Do Something return "some result"; } } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/230569.html
標籤:.NET技术
