Naming names - the "code clean way" reading notes (a)

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

Original Address: https://liujiao111.github.io/2019/06/18/clean-code/

Variable naming is the most basic part of the program, most people also easy to overlook the part, and believe that it is insignificant, the fact that otherwise, naming code can make people look good, clear. Here are some rules are naming the variable when it is appropriate to follow.

Veritable

Variables, functions, name of the class should be able to answer all the big questions, it tells you, why it exists, it is what you do, how we should use. If the name needs to be supplemented comment, it is not worthy of the name
, for example, the following code:

public static List<int[]> getThem(List<int[]> theList) {
        List<int[]> list1 = new ArrayList<>();
        for (int[] x : theList) {
            if(x[0] == 4) {
                list1.add(x);
            }
        }
        return list1;
    }

The code looks very simple, there is no wrong place, but the problem is the ambiguity of the code, the following specific issues:

  • What is theList;
  • What subscript 0 entries sense theList is?
  • What is the significance of the value of 4?
  • How I use the returned list?
    Improve:
  • If we develop a minesweeper game, theList grid unit represents a list of the disk, it will be renamed gameBoard
  • Subscript 0 is a state entry value 4 indicates "flagged" as long as it is meaningful to the name, the code will be improved considerably, and this data is returned
private static final int STATUS_VALUE = 0;
    private static final int FLAGGED = 4;

    public static List<int[]> getThem(List<int[]> theList) {
        List<int[]> flaggedCells = new ArrayList<>();
        for (int[] cell : theList) {
            if(cell[STATUS_VALUE] == FLAGGED) {
                flaggedCells.add(cell);
            }
        }
        return flaggedCells;
    }

Could go further without int array represents a cell, but another write a class that includes a isFlagged function, used to determine whether a cell has been labeled.
The new version of the code is as follows:

public static List<Cell> getThem(List<Cell> theList) {
        List<Cell> flaggedCells = new ArrayList<>();
        for (Cell cell : theList) {
            if(cell.isFlagged()) {
                flaggedCells.add(cell);
            }
        }
        return flaggedCells;
    }

Corresponding cell type (no code book, not necessarily accurate)

public class Cell {

    private Integer flag;

    private static final int FLAGGED = 4;
    
    public Cell(Integer flag) {
        this.flag = flag;  
    }

    private boolean isFlagged() {
        return this.getFlag() == FLAGGED;
    }


    public Integer getFlag() {
        return flag;
    }

    public void setFlag(Integer flag) {
        this.flag = flag;
    }
}

Can be seen, simply change it name, you can easily know what happened, this is the choice of the power of a good name.

Avoid misleading

  • Do not use the platform inside proper nouns variable names, such as hp, aix, sco etc;
  • Do not take the name of the List to represent a group of things, such as accountList, with accountGroup or bunchOfAccounts instead, even accounts are better
  • Discrimination using small named name;
  • Looks like a name with letters, numbers and uppercase letters such as the letter O 1 as a variable name, and 1 and letters I, because like O 0

Do meaningful distinction

  • Digital series named as (a1, a2, a3 ...), this is purely misleading name
  • Nonsense is a meaningless distinction, if there are a Product class ProductInfo or ProductData class, their names are different, but the meaning of info and data is no distinction

Use read out the name

Single letters and numeric constants in the document difficult to search you want, because there may be many, so a single variable method applies only to short local variable name length should correspond to the size of its scope, if the variable or constant in code multiple use, which name should be given to facilitate the search elements, such as the difference between 5 and WORK_DAYS_PER_WEEK

Interface and implementation

In front of the interface is not necessary to add a modified I

Avoid thinking maps

In addition to using the i or j loop counters among other places should avoid the use of single-letter variable name

The class name

Class names should be nouns or noun phrases, avoiding the use of verbs

Method name

Method names should be a verb or a verb phrase, such as postPayment, deletePage or save, the JavaBean plus get, set, is a prefix

Each concept corresponds to one word

Choose a word to each abstraction, and a consistent

Do not use puns

Avoid the same word used for different purposes. The code should try to make the reader understand the need popular easy to understand, and not need to bother digging academic mode

Use solutions Name

Try to use computer science terms, according to the field to command the issues involved may not be wise.

The problem with using the name of the field from involved

If you can not use the term in the field of computer science to name, then using the name of the field from the issues involved

Add meaningful context

Most names are not self-explanatory, on the contrary, if the good name of the class, function, or namespace to place them, provide context to the reader, so make others more clearly.
If a bunch of variables: firstName, lastName, street, houseNumber , city, state and zipcode variables, they put together quite clearly constitute an address, but if alone in seeing a state approach it? You do not think that the probability of a large part of the address is right? You can add a prefix addrFirstname, addrState etc., in order to provide context, a better solution is to create the Address class.

Do not add useless context

For example, there is a GSD of application, in which the front of each class plus a GSD prefix is ​​no need to work.

Free eBook share: link:
https://pan.baidu.com/s/1wvoRJGonA70J9hFn_w5jwA
extraction code: 37jy

Guess you like

Origin blog.csdn.net/qq_34464926/article/details/92796776