1. Use intetion-revealing Names

    Public class MeanifulName {/** * public class MeanifulName {/** * public class MeanifulName {/** * public class MeanifulName {/** * public class MeanifulName {/** * public class MeanifulName {/** * The method name does not indicate what the method does * 2. It does not indicate what theList stands for * 3. The subscript 0 in theList elements has any special meaning * 4. */ public static List<int[]> getThem(List<int[]> theList) {List<int[]> list1 = new ArrayList<>();for (int[] x : theList) {
                if(x[0] == 4) { list1.add(x); }}returnlist1; } /** * the following code is an improvement on the above: * 1. The method name indicates that the method is fetching the marked cell * 2. GameBoardCells indicates that it represents a checkerboard cell * 3. Use STATUS_VALUE to indicate that subscript 0 holds the state of the cell * 4. FlaggedCells public static List<int[]> getFlaggedCells(List<int[]> gameBoardCells) { final int STATUS_VALUE = 0; final int FLAGGED = 4; List<int[]> flaggedCells = new ArrayList<>();for (int[] cell : gameBoardCells) {
                if(cell[STATUS_VALUE] == FLAGGED) { flaggedCells.add(cell); }}returnflaggedCells; } /** * This code can be improved again with the encapsulation of Java classes, * the Cell class has an attribute inside that indicates the Cell state, Public Static List<Cell> getFlaggedCells(List<Cell> gameBoardCells) {List<Cell> flaggedCells = new ArrayList<>();for (Cell cell : gameBoardCells) {
                if(cell.isFlagged()) { flaggedCells.add(cell); }}return flaggedCellls;
        }
    
        static class Cell {
            private boolean flagged;
            public Cell() {
    
            }
            public boolean isFlagged() {
                returnflagged; }}}Copy the code
  2. Avoid Disinformation

    • Avoid arbitrary abbreviations
    • Avoid misleading writing: lowercase L and uppercase O, for example
  3. Use Meaningful Distinctions

    • If names must be different, then they should also mean something diffrent.
    Public static void copyChars(char[] a1, char[] a2) {public static void copyChars(char[] a1, char[] a2) {for(int i = 0; i < a1.length; i++) { a2[i] = a1[i]; }} /** *sourcePublic static void copyChars(char[])source, char[] destination) {
        for (int i = 0; i < source.length; i++) {
            destination[i] = source[i]; }}Copy the code
    • Noise words are another topic of distinction: For example, the class names ProductionInfo and ProductionData mean the same thing, but the different names make it seem like they need to be distinguished.
  4. Use pronounceable Names:

    class DtaRcrd102 {
        private Date genymdhms;
        private Date modymdhms;
        private final String pszqint = "102";
    }
    
    class Customer {
        private Date generationTimestamp;
        private Date modificationTimestamp;
        private final String recordId = "102";
    }
    Copy the code
  5. Use Searchable Names:

    For example, the number 7 is not easy to look up for days of the week, but after defining it as a meaningful variable, DAYS_PER_WEEK = 7 makes it easy to look up.

  6. Classes should be named using nouns or name phrases; Methods should be named using verbs or phrasal verbs

  7. Pick one word for one abstract concept and stick with it:

    For example, you can have the words get, retrive, fetch, and so on to express the action, but if you use get to express the action, then you should always use get, not get and fetch.

  8. Don’t Pun:

    For example, we have used add to express adding or concatenating two values, and when we need to express the concept of putting an object into a container, we need to re-select, Let’s say we have many classes where add will create a new value by adding or concatenating Two existing values. Now let’s say we are writing a new class that has a method that puts its single parameter into a collection. Should we call this method add? It might seem consistent because we have so many other add methods, but in this case, the semantics are different, so we should use a name like insert or append instead. To call the new method add would be a pun)

  9. Use Solution Domain Names or Use Problem Domain Names whenever possible

  10. Add Meaningful Context:

    public class MeanifulContext {
        static class UnclearContext {
            public static printGuessStatistics(char candidate, int count) {// These variables do not have a context meaning String number; String verb; String pluralModifier;if (count == 0) {
                    number = "no";
                    verb = "are";
                    pluralModifier = "s";
                } else if (count == 1) {
                    numbers = "1";
                    verb = "is";
                    pluralModifier = "";
                } else {
                    number = Integer.toString(count);
                    ver = "are";
                    pluralModifier = "s";
                }
                String guessMessage = String.format("There %s %s %s%s", verb, number, candidate, pluralModifier); System.out.println(guessMessage); }} static class GuessStatisticsMessage {// These variables are GuessStatisticsMessage private String number; private String verb; private String pluralModifier; public String make(char candidate, int count) { createPluralDependentMessageParts(count);return String.format("There %s %s %s%s", verb, number, candidate, pluralModifier); } private void createPluralDependentMessageParts (int count) {/ / by split method to simplify the code hereif (count == 0) {
                    thereAreNoLetters();
                } else if (count == 1) {
                    thereIsOneLetter();
                } else {
                    thereAreManyLetters(count);
                }
            }
    
            private void thereAreNoLetters() {
                number = "no";
                verb = "are";
                pluralModifier = "s";
            }
    
            private void thereIsOneLetter() {
                number = "1";
                verb = "is";
                pluralModifier = "";
            }
    
            private void thereAreManyLetters(int count) {
                number = Integer.toString(count);
                verb = "are";
                pluralModifier = "s"; }}}Copy the code
  11. Reference: [1] : Code cleanliness