C# Challenge 11 - 使用 C# 中的慣例、空白字元與註解,建立可讀取的程式碼

C# Challenge 11 - 使用 C# 中的慣例、空白字元與註解,建立可讀取的程式碼

Microsoft 2023 年所提供的 C# codecamp 基礎課程,總共有 38 個單元,完成後就可以獲得 Certification ,今天要來跟大家分享的是第 11 單元的內容。

本節內容

常見的變數命名規範

  • 駝峰式命名 string thisIsCamelCase;
  • 絕對不要簡寫,因為除了你自己以外的人會看不懂

好的範例:

1
2
3
4
5
6
7
char userOption;

int gameScore;

float particlesPerMillion;

bool processedCustomer;

怎麼在編輯器中註解

第一種

單行的註解 //

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Random random = new Random();
string[] orderIDs = new string[5];
// Loop through each blank orderID
for (int i = 0; i < orderIDs.Length; i++)
{
// Get a random value that equates to ASCII letters A through E
int prefixValue = random.Next(65, 70);
// Convert the random value into a char, then a string
string prefix = Convert.ToChar(prefixValue).ToString();
// Create a random number, pad with zeroes
string suffix = random.Next(1, 1000).ToString("000");
// Combine the prefix and suffix together, then assign to current OrderID
orderIDs[i] = prefix + suffix;
}
// Print out each orderID
foreach (var orderID in orderIDs)
{
Console.WriteLine(orderID);
}

第二種

多行的註解 /* */

1
2
3
4
5
/*
string firstName = "Bob";
int widgetsPurchased = 7;
Console.WriteLine($"{firstName} purchased {widgetsPurchased} widgets.");
*/

空白字元

適度的留空白行比較方便開發人員去做編輯和修改ㄛ!


重點整理

這個章節算是開發習慣的一個大方向建議,裡面有特別提到關於註解的部分也不要完全相信,因為有時候邏輯或是內容有替換但是忘記修改就會發生認知錯誤,所以記得還是要自己去釐清一下該功能的邏輯後再去做新的開發或者是維護。
那我們下次見ʘ‿ʘ


參考資料

評論