1.代碼儘量的短
是指在橫向上的短,每行儘量不要超過那條豎線
例如
2.減少大括號嵌套層數,一般不要超過3層
合理使用continue,return, break,儘早退出循環
例如:
$t_sum = 0;
$t_param = 'xxxx';
for($i = 0; $i < 100; $i++){
if(1 == $i % 2){
if($t_param == 'name'){
$t_sum += $i;
//...更多其他複雜操作
}
}
}
改造後:
$t_sum = 0;
$t_param = 'xxxx';
for($i = 0; $i < 100; $i++){
if(1 != $i % 2){
continue;
}
if($t_param != 'name'){
continue;
}
$t_sum += $i;
//...更多其他複雜操作
}
例子2:
function test_function($vParam){
if('name' == $vParam){
if(...){
//其他操作
}
}
}
改造後:
function test_function($vParam){
if('name' != $vParam){
return;
}
if(...){
//其他操作
}
}
3.參數簡單化
函數的參數儘量簡單,不要在參數裏面做複雜的取值、比較。
除了鏈式操作的場景之外,建議都簡化參數。
能抽取出來形成一個變量的,就形成一個變量,並加上註釋。
理由:減少人的思考,一眼就能明白是什麼意思,減少出錯的機會。
不好的例子(1):
getUserDataByUserName(getUserNameByUserId(getUserId(), 'GOODS'));
建議的例子:
$tUserId = getUserId();
$tUserName = getUserNameByUserId($tUserId, 'GOODS');
$tUserData = getUserDataByUserName($tUserName);
不好的例子(2):
TestFunction( (($user_type > 100) || ( $param == 'name')) ? 888 : 999, getDataById()['dataList'][0]['user_name'], strtoupper(trim($t_company_name)));
建議寫法:
//用戶類型數據, 如果用戶類型是什麼或者參數是什麼就得的什麼值
$t_user_type_data = (($user_type > 100) || ( $param == 'name')) ? 888 : 999;
//用戶名
$t_user_name = getDataById()[0]['dataList'][0]['user_name'];
//大寫公司名
$t_uppper_company_name = strtoupper(trim($company_name));
TestFunction($t_param_data, $t_user_name, $t_uppper_company_name);
不好的例子(3):
if(stristr($t_field_type, 'decimal')
|| stristr($t_field_type, 'float')
|| stristr($t_field_type, 'double')
|| stristr($t_field_type, 'number')
|| stristr($t_field_type, 'numeric')){
//...
}
建議寫法:
//是小數嗎
$t_is_float = stristr($t_field_type, 'decimal')
|| stristr($t_field_type, 'float')
|| stristr($t_field_type, 'double')
|| stristr($t_field_type, 'number')
|| stristr($t_field_type, 'numeric');
if($t_is_float){
//...
}
4.變量命名適度複雜
除了循環變量i,j,k之類的,儘可能的長一點,取有意義的名字
好處:
- 避免變量不小心覆蓋
- 以後見到變量名就知道是什麼意思
- 搜索變量名能快速找到
例如:
不建議的寫法:( $sql 重名了)
$sql = 'UPDATE table_1 SET name='xxx' WHERE id = 888';
db()->execute($sql);
$sql = 'SELECT * FROM table_1 ';
db()->query($sql);
建議的寫法:
$t_update_sql = 'UPDATE table_1 SET name='xxx' WHERE id = 888';
db()->execute($t_update_sql);
$t_data_sql = 'SELECT * FROM table_1 ';
db()->query($t_data_sql);
5.變量的命名(賦值)與使用盡可能靠近
理由:
(1) 防止中間被不小心重新賦值了
(2) 防止還要思考是什麼意思
$t_param_type = $this->getParamType();
//...
//...
//...
//...
//...
//...
//...
//...
//...
//...
//...
//...
//...
//...
if('GOODS' == $t_param_type){
//...
}
建議寫法:
//...
//...
//...
//...
//...
//...
//...
//...
//...
//...
//...
//...
//...
//...
$t_param_type = $this->getParamType();
if('GOODS' == $t_param_type){
//...
}
6.變量前綴建議:
本地變量(局部變量): t, 例如 t_user_id
數組可以以arr_開頭, 如 arr_users
全局變量:g,例如 g_user_id
函數參數:v,例如 v_user_id
理由:一見到變量名,就知道從哪裏來
7.與常量比較,常量在前
例如:
if(0 == $t_status){
}
理由: 防止誤操作把==寫成=
8.判斷語句再簡單也要把大括號加上
例如:
if($user_id == 88) incOrderNum(88);
建議的寫法:
if(88 == $t_user_id){
incOrderNum(88);
}
理由:
防止後添加的語句不屬於該判斷條件
if($user_id == 88) incOrderNum(88); decWarehouseNum(88);
正確的寫法:
if(88 == $t_user_id){
incOrderNum(88);
decWarehouseNum(88);
}
9.函數前面添加註釋
在函數定義前面,輸入 /** 然後回車,phpstorm就會自動把註釋模板創建好。
對程序員來說,寫文檔是不喜歡的,如果函數還不加註釋,那就太給自己和別人挖坑了。
在複雜的邏輯前面,也要儘量加上註釋。
理由:
好記性不如爛筆頭。
/**
* 刪除動態報表數據
*
* @param $report_id :報表id
* @param $v_pk_rows :待刪除的行
* @param $check_online :檢查是否在線
*
* @return array
*/
public function delDynReportData(
$report_id,
$v_pk_rows,
$check_online = true
) {
...
}
10.避免拼寫錯誤
有些時候一個字符拼寫錯誤,可能導致嚴重的後果,至少會給其他人造成困惑。
特別是比如把字母l 寫成1的。 一個變量名命名錯誤,其他人用正確的拼寫就搜不到這個變量了。
11.採用統一的代碼縮進
在phpstorm裏面,點擊菜單Code=>Reformat Code,就可以把代碼的縮進整理得井井有條,清清爽爽。