<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] # 有意義的命名 ## 命名原則 ### 1. 名副其實 變量、函數或類的名稱應該已經答復了所有大問題。如果還需要注釋來補充,就不算名副其實。 > Bad ```java int d; //消釋的時間,以日計 ``` > Good ```java int elapsedTimeInDays; int daysSinceCreation; int daysSinceModification; int fileAgeInDays; ``` 選擇體現本意的名稱能讓人更容易理解和修改代碼。 > Bad ```java public List<int[]> getThem() { List<int[]> list1 = new ArrayList<int[]>(); for (int[] x: theList) { if (x[0] == 4) { list1.add(x); } } return list1; } ``` - **theList類型不明** - **theList下標意義不明** - **常量4意義不明** - **不知如何使用返回列表** > Good 比如代表掃雷 獲取`已標記`的單元格信息 ```java public List<int[]> getFlaggedCells() { List<int[]> flaggedCells = new ArrayList<int[]>(); for (int[] cell : gameBoard) { if (cell[SYATUS_VALUE] == FLAGGED) { flaggedCells.add(cell); } } return flaggedCells; } ``` > 優化版 ```java public List<Cell> getFlaggedCells() { List<Cell> flaggedCells = new ArrayList<Cell>(); for (int[] cell : gameBoard) { if (cell.isFlagged()) { flaggedCells.add(cell); } } return flaggedCells; } ``` ### 2. 避免誤導 避免使用與本意相悖的詞。 ### 3. 做有意義的區分 > Bad - a1 a2 a3 等數字系列命名 - 已經有了Product類,又有了ProductInfo和ProductData... 廢話都是冗余。Variable一詞永遠不應當出現在變量名中。Table一詞永遠不應當出現在表名中。 > Bad - getActiviveAccount(); - getActiviveAccounts(); - getActiviveAccountInfo(); ### 4. 使用讀得出來的名稱 ### 5. 使用可搜索的名稱 單字母名稱和數字常量有個問題,就是很難在一大篇文字中找出來。 `WORK_DAYS_PER_WEEK`要比數字`5`好找的多 ### 6. 避免使用編碼 - 匈牙利語標記法 X - 成員前綴 X - 接口和實現 X(前導字母I被濫用) ### 7. 避免思維映射 ### 8. 類名 類名和對象名應該是名詞或名詞短語。 > Good `Customer`、`WikiPage`、`Account`.etc > Bad `Manager`、`Processor`、`Data`、`Info` ### 9. 方法名 使用動詞或動詞短語。 `postPayment`、`deletePage`或`Save` ```java Complex fulcrumPoint = Complex.FromRealNumer(23.0); ``` 通常好于 ```java Complex fulcrumPoint = new Complex(23.0); ``` ### 10. 別扮可愛 不要使用俗語或俚語。 ### 11. 每個概念對應有一個詞 每個抽象概念選一個詞,并且一以貫之。 > Bad `fetch`、`retrieve`和`get`來給在多類中的獲取數據命名,不易記憶。 一堆`controller`中又有`manager`還有`driver`,就會令人困惑。 ### 12. 別用雙關語 ### 13. 使用解決方案領域名稱 使用CS術語、算法名、模式名、數學術語等等 ### 14. 使用源自所涉問題領域的名稱 ### 15. 添加有意義的語境 > Bad ```java private void printGuessStatistics(char candidate, int count) { Stirng number; String verb; String pluralModifier; if (count == 0) { number = "no"; verb = "are"; pluralModifier = "s"; } else if ( count == 1 ) { number = "1"; verb = "is"; pluralModifier = ""; } else { number = Integer.toStirng(count); verb = "are"; pluralModifier = "s"; } String guessMessage = String.format( "There %s %s %s%", verb, number, candidate, pluralModifier ); print(guessMessage); } ``` > Good ```java public class GuessStatisticsMessage { private String number; private String verb; private String pluraModifier; public String make(char candidate, int count) { createPluralDepentMessageParts(count); return String.format( "There %s %s %s%s", verb, number, candidate, pluraModifier ); } private void createPluralDepentMessageParts(int count) { if (count == 0) { thereAreNoLetters(); } else if (count == 1) { thereIsOneLetter(); } else { thereAreManyLetters(count); } } private void thereAreManyLetters(int count) { number = Integer.toString(count); verb = "are"; pluraModifier = "s"; } private void thereIsOneLetter() { number = "1"; verb = "is"; pluraModifier = ""; } private void thereAreNoLetters() { number = "no"; verb = "are"; pluraModifier = "s"; } } ``` ### 16. 不要添加沒用的語境 # 函數 - 短小 20行封頂最佳 - 只做一件事
                  <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>

                              哎呀哎呀视频在线观看