排版

程式碼越靠左邊就越平易近人

限制每行字數

寫程式推薦的寬度是每行 80 ~ 120 字,即便有外接大螢幕,也不建議超過這個數字。


換行

程式碼越往右邊就意味著該行要判斷的事情越複雜,且不容易閱讀上下文

// bad code, 條件太長了
if ($conditions['status'] === true && $conditions['is_test'] === false && $conditions['type'] === 'boolean') {
    echo 'hello';
}
// good code, 換行,這種方式還能寫註解
if ($conditions['status'] === true
 && $conditions['is_test'] === false
 && $conditions['value'] === 'hello') {
    echo 'hello';
}
// good code, 萃取出新的方法
if ($this->canEcho($conditions)) {
    echo 'hello';
}

使用匿名函式


三元運算子正確用法

如果結果是單數,請換行

如果是 null 或 array,可以這樣寫

如果輸出結果是兩組陣列,還是建議抽出去做


使用雙引號或 Heredoc

雙引號和 Heredoc 可以解析變數,如果沒有變數需求則使用單引號

Last updated

Was this helpful?