<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [TOC] > [參考](http://www.yinwang.org/blog-cn/2015/11/21/programming-philosophy) ## 讀代碼的次數要多與寫代碼 所有不要了強行精簡,而喪失語義 ## 不要做復雜的復用 共同點少于不同點,則寫兩個函數 error ``` void foo() { if (getOS().equals("MacOS")) { a(); } else { b(); } c(); if (getOS().equals("MacOS")) { d(); } else { e(); } } ``` good ``` void fooMacOS() { a(); c(); d(); } //和 void fooOther() { b(); c(); e(); } ``` ## 提取相同部分 error ``` void foo() { a(); b() c(); if (getOS().equals("MacOS")) { d(); } else { e(); } } ``` good ``` void preFoo() { a(); b() c(); } void fooMacOS() { preFoo(); d(); } void fooOther() { preFoo(); e(); } ``` ## 何時需要注釋 好的代碼是不需要寫邏輯注釋的,只需要寫業務注釋 ## 局部變量可簡單 error ``` boolean successInDeleteFile = deleteFile("foo.txt"); if (successInDeleteFile) { ... } else { ... } ``` good ``` boolean success = deleteFile("foo.txt"); if (success) { ... } else { ... } ``` ## 不要重用局部變量 error ``` String msg; if (...) { msg = "succeed"; log.info(msg); } else { msg = "failed"; log.info(msg); } ``` good ``` if (...) { String msg = "succeed"; log.info(msg); } else { String msg = "failed"; log.info(msg); } ``` ## 把復雜的邏輯提取出去,做成“幫助函數” error ``` //code // put elephant1 into fridge2 openDoor(fridge2); if (elephant1.alive()) { } else { } closeDoor(fridge2); //code ``` good ``` void put(Elephant elephant, Fridge fridge) { openDoor(fridge); if (elephant.alive()) { } else { } closeDoor(fridge); } //code put(elephant1, fridge2); //code ``` ## 在合理的地方換行 error ``` if (someLongCondition1() && someLongCondition2() && someLongCondition3() && someLongCondition4()) { ... } ``` good ``` if (someLongCondition1() && someLongCondition2() && someLongCondition3() && someLongCondition4()) { ... } ``` ## 避免使用自增減表達式 如 `(i++,++i,i–,–i)` ``` //error foo(i++) //good int t = i; i += 1; foo(t); ``` ``` //error foo(++i) //good i += 1; foo(i); ``` 自增在 一下兩種可正常使用 1. `for(int i = 0; i < 5; i++)` 2. 單獨寫層一行 `i++` ## 不要在條件語句時,省略大括號 error ``` if (...) action1(); ``` 由于花括號的存在,使得代碼界限明確,讓眼睛負擔更小了 ## 合理使用括號,不要依賴操作符 `1 + 2 * 3` ok `2 << 7 - 2 * 3` error `2 << (7 - 2 \* 3)`good ## 避免使用continue和break 循環語句(for,while)里面出現return是沒問題的,然而如果你使用了continue或者break,就會讓循環的邏輯和終止條件變得復雜,難以確保正確 1. 如果出現了continue,你往往只需要把continue的條件反向,就可以消除continue。 2. 如果出現了break,你往往可以把break的條件,合并到循環頭部的終止條件里,從而去掉break。 3. 有時候你可以把break替換成return,從而去掉break。 4. 如果以上都失敗了,你也許可以把循環里面復雜的部分提取出來,做成函數調用,之后continue或者break就可以去掉了。 案例1: error ``` List<String> goodNames = new ArrayList<>(); for (String name: names) { if (name.contains("bad")) { continue; } goodNames.add(name); ... } ``` good ``` List<String> goodNames = new ArrayList<>(); for (String name: names) { if (!name.contains("bad")) { goodNames.add(name); ... } } ``` 案例2: error ``` while (condition1) { ... if (condition2) { break; } } ``` good ``` while (condition1 && !condition2) { ... } ``` 案例3: error ``` public boolean hasBadName(List<String> names) { boolean result = false; for (String name: names) { if (name.contains("bad")) { result = true; break; } } return result; } ``` good ``` public boolean hasBadName(List<String> names) { for (String name: names) { if (name.contains("bad")) { return true; } } return false; } ``` ## 寫直觀的代碼 error ``` if (action1() || action2() && action3()) { ... } ``` good ``` if (!action1()) { if (action2()) { action3(); } } ``` ## 不漏下所有的條件 由于疏忽而漏掉的分支,全都會自動“掉下去”,最后返回意想不到的結果 error ``` if (...) { if (...) { ... return false; } } else if (...) { ... return false; } return true; ``` good ``` if (...) { if (...) { ... return false; } else { return true; } } else if (...) { ... return false; } else { return true; } ``` ## 不省略缺失值 error ``` String s = ""; if (x < 5) { s = "ok"; } ``` good ``` String s; if (x < 5) { s = "ok"; } else { s = ""; } ``` ok(如果請情況簡單) ``` String s = x < 5 ? "ok" : ""; ``` ## 錯誤處理 不要漏掉任何一個可能返回錯誤的 error 如果foo和bar都可能產生異常A,你的代碼應該盡可能寫成 error ``` try { foo(); bar(); } catch (A e) {...} ``` good ``` try { foo(); } catch (A e) {...} try { bar(); } catch (A e) {...} ``` ## 處理 null 當 find 返回 null 時候,需要對 null 有意義的處理 error ``` public String foo() { String found = find(); if (found == null) { return null; } } ``` good ``` public String foo() { String found = find(); if (found == null) { //code } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看