CPE 程式碼撰寫標準

Coding Standards
1.空白的使用

運算元 ,標點符號,旁邊用一個空白隔開
int a, b;
a = 8 + 4;
b = (6 + 7) * 8;

邏輯判斷符號 (等於,不等於,或) 也用一個空白隔開
if (expression == true)

for迴圈內的分號後用空白隔開
for (int val = 1; val <=10; val++)

2.括弧,縮排的使用

每層內縮都應該縮進 4 個空白,"左大括號跟著上一行,而右大括號自成一行"。
if (something) {
    ....
}

3.變數的宣告使用
程式中若使用常其名稱使用大寫表示
const int PI = 3.14159;
int MAX = 1000; //錯誤


區域變數使用駝峰式命名法,第一個單字以小寫字母開始;第二個單字的首字母大寫
string name = “lucifer”; //一個單字使用小寫命名
string secondName =“michael”; //若有二個或多個單字連結在一起,第二個單字的首字母大寫,利用駝峰式命名法來表示,可以增加變數和函式的可讀性。

若要使用指標請在變數前面加上一個小寫p
String *pName= new String;

4."( )"的使用

關鍵字後(keyword)後應該使用一個空白在接括號()
if (condition) //正確,if後有一個空白
while (condition) //正確,while後有一個空白
for(condition) //錯誤,for後面無空白,會使for看起來像一個函式

函式(finction)後不使用空白
printf("i like monster-strike!"); //正確,printf為函式後面不接空白

5.if,while,for等關鍵字後只有一個判斷式,統一不使用括號
if (condition)
    somevalue = 2;

for (condition)
    continue;

參考 :
C++ Coding Standard : http://www.possibility.com/Cpp/CppCodingStandard.html