前往
大廳
主題

【學習筆記】Coding Style 打程式的一些命名規範,(C#)

%%鼠 拒收病婿 | 2021-06-07 00:53:43 | 巴幣 424 | 人氣 2150

前言:
被說程式碼像是多重人格打的,感謝大佬推薦文章,我也額外看了一些。
整理的規範不只C#也能用,只是主要讀的是C#。

本篇主要是重點整理(懶人包),太繁瑣的就沒看了。

[1]C# Coding Style
  • consistency(統一性)最重要
  • 大括號使用Allman style
    • 大括號{獨立一行 (each brace begins on a new line.)
  • 縮排使用4個space,不使用tab
  • 內部與私有變數使用camelCase,底線「_」開頭,且盡可能使用readonly。
  • static私有變數使用 「s_」開頭。
  • thread static 變數使用 「t_」開頭。 (readonly 和 static的順序應該是:static readonly)
  • public變數要謹慎使用,且使用PascalCasing,且不使用前綴(prefix)詞。
  • PascalCasing(大寫頭)和camelCase(小寫頭)的差異。
  • 避免使用this。
  • 總是打出可見度(visibility),例:就算預設是private不用打,但請都打出來。
  • using Namespace 要照英文字母順序排列,除了System.*一定要放在最上面。
  • 避免一個以上的空行。
  • 避免無用的空白鍵,可以開啟VS的檢視空白功能。"View White Space (Ctrl+R, Ctrl+W)" or "Edit -> Advanced -> View White Space"
  • 若一個file已存在另一個編碼風格,以那個風格為優先(precedence)。
  • 只在類型明確(explicitly)打上的時候使用「var」。例:
    • (O) var stream = new FileStream(...)
    • (X) var stream = OpenStandardInput()
  • 使用語言基礎型態宣告(language keyworads),而不是BCL(Base class library)。例:
    • (O) int , string ,float ,int.Parse
    • (X) Int32 , String , Single , Int32.Parse
  • 本地常數變數使用PascalCasing,唯一的例外是interop的常數變數。(第12條看不太懂 )
  • 方法名都使用PascalCasing (包含本地方法)。
  • 變數宣告總是要放在類型宣告(type declarations)的最上方。
  • 當引入非ASCII時,使用Unicode escape sequences (\uXXXX)。
  • 標籤要額外1縮排。When using labels (for goto), indent the label one less than the current indentation.
  • 單行(single-statement)規則:
    • 絕不使用單行程式,例如:if (source == null) throw new ArgumentNullException("source");
    • 歡迎使用大括號,例如if/else if/else ....
    • 大括號也許可以被省略如果內容的"所有" if else都只有一行。
  • 讓所有內部與private類型static或sealed,除非一定要引用到他們。可以先設成private,未來要引用到再改。


[2] C# at Google Style Guide
  • class / method / enum / 公開變數 / namespace:PascalCase
  • 本地變數:camelCase
  • private, protected, internal 變數:_camelCase
  • 縮寫也是,
    • (O)MyRpc
    • (X)MyRPC
  • 介面使用I開頭,例:IInterface
  • 檔案命名:PascalCase,例:MyFile.cs
  • 檔案名稱盡量要跟main class一樣
  • 正常來說,偏好一個檔案一個核心class
  • Modifiers順序:
    1. public
    2. protected
    3. internal
    4. private
    5. new
    6. abstract
    7. virtual
    8. override
    9. sealed
    10. static
    11. readonly
    12. extern
    13. unsafe
    14. volatile
    15. async
  • using Namespace 要照英文字母順序排列,除了System.*一定要放在最上面。
  • 類別內容的順序:
    1. 巢狀類別、enum、委派、事件(Nested classes, enums, delegates and events.)
    2. Static, const and readonly fields.
    3. Fields and properties.
    4. Constructors and finalizers.
    5. Methods.
    • 其每個內容內的順序是:
      1. Public.
      2. Internal
      3. Protected internal.
      4. Protected
      5. Private
    • 盡量把interface的實作都放在一起

  • 一行只能最多一個陳述句(A maximum of one statement per line.)
  • 一個statement 只執行一個任務。(A maximum of one assignment per statement.)
  • 縮排使用2個空白替代tab。 (不要用tab)
  • 欄位最多100 (?   (Column limit: 100.)
  • 開頭大括號前面不要有line break.  (No line break before opening brace.)
  • 結尾大括號跟下個else之間不要有line break. (No line break between closing brace and else.)
  • 即使大括號是不必要的,還是要盡量使用。(Braces used even when optional.)
  • if /for /while 與逗號等符號後加上 space  (Space after if/for/while etc., and after commas.)
  • 開頭小括號後方與結尾小括號前方不要加space。 (No space after an opening parenthesis or before a closing parenthesis.)
  • unary operator(例: +-運算符號) 與 operand之間不要加space,其他operator 與operand 之間要加space。
  • 斷行後接續的使用4個space做縮排。(In general, line continuations are indented 4 spaces.)
  • 大括號的斷行不算是 line continuations。(Line breaks with braces (e.g. list initializers, lambdas, object initializers, etc) do not count as continuations.)
  • 方法帶入的參數如果太多,可以拆成好幾行,且每行要對齊第一個參數。
  • 宣告變數考慮順序:const > readonly
  • input: 盡量最嚴格的集合類型,例:IReadOnlyCollection / IReadOnlyList / IEnumerable
  • output: 若是傳址,偏好使用IList大於IEnumerable,若是傳值,偏好最嚴格的。(For outputs, if passing ownership of the returned container to the owner, prefer IList over IEnumerable. If not transferring ownership, prefer the most restrictive option.)
  • Structs跟Class差別:
    • Structs輸入輸出都是傳值
    • 永遠都優先使用class (Almost always use a class.)
    • 當資料生命週期短的時候才考慮使用struct,例Vector3
    • 以上不是絕對的,有些團隊會因為效率考量偏好使用Structs。
  • 一般建議變數初始化。(Field initializers are generally encouraged.)
  • out放在參數最後
  • 謹慎使用ref,不要以為ref搭配Structs是種優化。(Do not use ref as an optimisation for passing structs.)
  • 使用委派(delegates)時,建議使用SomeDelegate?.Invoke()。  (null conditional operator)



單字筆記:
brace:大括號{、牙套
spurious:虛偽的
precedence:優先
explicitly:明確的
interop:?
conventions:公約
omit:省略
derivation:引出
parenthesis:小括號 ( )
unary:一元
unary operator:例如+、-運算
operand:操作數。 例如1+1, 1是操作數
immutable:不可變的
Generator code:?


後記:
抱歉有點機翻,不過是確實都有一行一行看過
((不太清楚thread static 是什麼

以下也想順便紀錄自己的Unity宣告規則,以便未來統一,若有建議或錯誤歡迎提出
event: OnDoMyEvent
coroutine:c_myCoroutine
IEnumerator:DoSthCoro()
Dictionary:myDataMap、私有:m_myData

其他:想到再補...... (建表格好了)
類型 public private private static
event OnDoSth _OnDoSth s_OnDoSth
coroutine myCoroutine c_myCoroutine s_c_myCoroutine
IEnumerator DoSthCoro() DoSthCoro() DoSthCoro()
Dictionary myDataMap m_myData s_m_myData



另外我有時覺得名稱太長,或組成名稱的單字每個都很短,很難閱讀時會喜歡用「_」區隔,例如: Do_This_Sth(),但好像要統一改成:DoThisSth()比較好?


送禮物贊助創作者 !
0
留言

創作回應

%%鼠 拒收病婿
發現更棒的微軟懶人包= =
https://docs.microsoft.com/zh-tw/dotnet/standard/design-guidelines/naming-guidelines
2021-06-07 01:03:47
樂小呈
窩就是所有 private 都不打的[e7]
2021-06-07 06:58:26
%%鼠 拒收病婿
這也是個風格XD
2021-06-07 11:36:01
樂小呈
是說我到現在都沒有查那麼系過诶,蒐藏一下XD
2021-06-07 06:59:01
在意你就輸了
我一直被程式大佬罵亂命名,實在很對不起他= =
2021-06-07 11:12:20
%%鼠 拒收病婿
當下雖然很不服氣,但之後都會很感謝有大佬願意罵我[e12]
2021-06-07 11:36:30
追蹤 創作集

作者相關創作

更多創作