Some time ago, I read the book “Refactoring”. I feel that I have gained a lot, some of which I have already learned, and some of which will make me understand. Here I will sort out my understanding of the coding method step by step. Although I finished reading the book, it still takes some time to sort it out completely according to my own understanding. Therefore, before the final completion, the structure and content of this article may be messy, but it may be more and more structured at any time, making it more logical. Due to its own coding style, there are also many places that will be described in conclusive language.

Why write development code with refactoring in mind?

I think it’s a good personal practice to always develop with the code in mind that it will be used somewhere else in the future, and at this stage try to separate the business code from the tool code, which has nothing to do with the business directly.

Will, of course, it is difficult to do it perfectly, such as writing a cost calculation engine, the ingredients of the business itself has some tools inside, but it can’t again from “expenses”, so it can only serves as a tool to use within a certain scope, this time should use the module or directory will be the following code to this business, so there are tools usage scenarios, After all, wrench is mainly screw, perfume is mainly hair nozzle.

JS can define a function in any scope. This is equivalent to defining a utility function in the current scope, which can only be used in the current scope and subscope.

For Java writers, use the public, protected, default, private qualifiers to limit the valid scope of a class, method, or field. For example, AbstractStringBuilder is a class that uses the default modifier (in fact, no modifier at all) and we don’t even know it exists because it is only available in the current package (java.lang). Or afterNodeAccess, a HashMap method that is also modified by default, can be implemented in LinkedHashMap (although I think it is better to use protected as it is sometimes inconvenient to implement some of the functions myself).

What is refactoring?

Reconstruction explains it in terms of noun and verb respectively:

  • N: an internal adjustment to software that improves understandability and reduces the cost of modification without changing the observable behavior of the software.
  • Verb: To use a series of techniques to adjust the structure of software without changing its observable behavior.

There are two important words in both sentences: observable behavior and intelligibility. The code is given to a computer to execute, but it is read by a human. Computers are desperate, and as long as the code they know executes correctly, this is observable behavior — making sure the inputs and outputs are the same before and after modification.

In people’s eyes, “people” should be called “people” or “person” and “execution” should be called “run” or “execute”. This is part of the comprehensibility. Personally, I think the comprehensibility to a large extent (60% to 70% based on feeling) is the name of semantic variable.

So what’s the other part of comprehensibility? Here I think the way to understand it is, a Porsche 911, it’s a Porsche, it’s a 911, it has four wheels… These are its properties (fields); In addition, it can drive, which can be considered as the method; It can also change gears, which can be considered a parameter method. This is another part of understandability: do your own thing and stay within boundaries. In fact, such embodiment is not only writing code, a person, a company is not the same, when the company is small, a receptionist may also be responsible for human resources, when the company is big, the department’s tasks are more and more, the responsibilities are more and more clear, then there are personnel, finance, each to perform their own duties. You and your sister wear the same pants when you’re a kid, and it’s kind of creepy when you’re older. There is no way in the world, but when more people walk, it becomes a way.

public class Car {
    private String brand = "Porsche";
    private String model = "911";
    private int countOfWheel = 4;
    
    public void runPublic void run(int level){// run(int level)}}Copy the code

In general, observable behavior and intelligibility speak to computers and humans, respectively, to be understood by computers and understood by humans.

Why refactor?

After explaining what refactoring is, it is easy to understand why refactoring is so simple that it can be understood by both computers and humans.

There’s a passage in Refactoring that I think explains it better:

A lot of superior programming is talking to a computer: you write code to tell the computer what to do, and it responds by doing exactly what you tell it to do. You have to fill in the gap between what you want him to do and what you tell him to do. The core of this programming model is “say exactly what I want.”

The last word is “say exactly what I want.” When you communicate with an object, you should tell it what I want according to its ability. How to understand this sentence?

You have to be abstract. What is JS capable of? It can make Ajax requests so that it can be used to access remote resources; What is Java capable of? It can multithread, it can access databases,