標籤:style color os io re c
類的大括弧在後面 不是另起一行
變數名首字母小寫 駝峰模式 [a-z][a-zA-Z0-9]*
注釋要另起一行,而不是跟在代碼後面,
移除注釋的程式碼片段要
swtich 至少包含3個case 否則就用if吧
if等不能嵌套超過3次
類中的方法不能超過20個,超過的話 就拆分把
移除沒有用的參數
移除沒用的變數
if必須要跟else
if總是跟著大括弧
代碼中不要有太多的return
switch 要加default
如下代碼
if (condition) { return true;} else { return false;}//或者if(a==b){return true;}else{return false;}應該寫成return condition;return a==b;
//直接返回function compute_duration_in_milliseconds() { $duration = ((($hours * 60) + $minutes) * 60 + $seconds ) * 1000 ; return $duration;}Compliant Solutionfunction compute_duration_in_milliseconds() { return ((($hours * 60) + $minutes) * 60 + $seconds ) * 1000;}
//出現重複參數function run() { prepare(‘action1‘); // Non-Compliant - ‘action1‘ is duplicated 3 times execute(‘action1‘); release(‘action1‘);}//正確的做法ACTION_1 = ‘action1‘;function run() { prepare(ACTION_1); execute(ACTION_1); release(ACTION_1);}
//布爾值直接判斷if ($booleanVariable == true) { /* ... */ }if ($booleanVariable != true) { /* ... */ }if ($booleanVariable || false) { /* ... */ }doSomething(!false);Compliant Solutionif ($booleanVariable) { /* ... */ }if (!$booleanVariable) { /* ... */ }if ($booleanVariable) { /* ... */ }doSomething(true);