类的大括号在后面 不是另起一行
变量名首字母小写 驼峰模式 [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 Solution
function 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 Solution
if ($booleanVariable) { /* ... */ }
if (!$booleanVariable) { /* ... */ }
if ($booleanVariable) { /* ... */ }
doSomething(true);