#代碼規(guī)范(從code review總結(jié))
1. 沒有使用filterStartLimit過濾limit字段值.filterStartLimit作用是防止sql注入.? orderby, page(分頁), groupby等參數(shù)都有類似風(fēng)險(xiǎn)拓萌。
2. 代碼里不應(yīng)出現(xiàn)magic number, 應(yīng)該定義常量增加可讀性
3. 路由URL中的"/"應(yīng)該用來體現(xiàn)層級而不是用來劃分單詞掩浙。
/offline/course/{id}/picture? ? #應(yīng)該使用? /offline_course/{id}/picture
4. 異常需要分類 不能都簡單拋出serviceException
Topxia\Common\Exception 中已經(jīng)有部分Exception類型矾兜,根據(jù)業(yè)務(wù)選擇
5. 檢查代碼的無用邏輯
如在controller層調(diào)用search時(shí),如果searchUserCount為0顽素,可以避免searchUsers
6. dao層sql 查詢 orderby 需要限制可排序字段(防注入)谷市。
7. search方法使用的場景是當(dāng)查詢條件不確定
conditions ?= array( 'parentId' => 0 ); 應(yīng)該使用 findXXXbyparentId(findParentXXX)
8. 變量命名冗余蛋哭,enrollmentPlan => plan运沦,增加可讀性
就像在CourseController下 不是courseManageAciton 應(yīng)是manageAciton,在這個(gè)語境下manage自然被理解為是管理Course泵额。
9. ‘if-else’循環(huán)內(nèi)容復(fù)雜,應(yīng)當(dāng)簡潔携添,考慮使用‘key => value’嫁盲。
多重循環(huán)嵌套時(shí),可以考慮是否可以用鍵值對形式來實(shí)現(xiàn)烈掠,或者用拼接的方法實(shí)現(xiàn)羞秤。
$status = array(
'new' => 'admin_enrollment_declare',
'declareing' => 'admin_enrollment_audit',
'assigned' => 'admin_enrollment_update_classroom',
);
if (!empty($plan) && $plan['status'] != 'published') {
return $this->redirect($this->generateUrl($status[$plan['status']] ,array('id' => $plan['id'])));
}
10. 在Controller中,不可暴露數(shù)據(jù)庫信息如”depth=> 2”
11. 字段”created_time”和”updated_time”左敌,不應(yīng)寫在service(新框架不用賦值瘾蛋,原先框架應(yīng)寫在dao)
12. 數(shù)據(jù)庫字段命名駝峰和下劃線皆可 ?userId user_id
13. 方法的命名不能太籠統(tǒng),要盡量有具體的含義矫限。
validateAuthority()? 修改為=> tryManageClassroomThread()