"Reconstruction (Second Edition)" Reading Notes (3)

The smell of bad code

How to find the corresponding bad code is also a method

Regarding bad code, there is actually no precise rule, only relative habits. Some people can tolerate it, while others cannot accept it. This is why a piece of code is different in everyone's eyes.

The author gives some judgment criteria in this chapter. Whether you are willing to accept them depends on your personal habits.

mysterious naming

As it is said, a good naming is always important, don’t make such nonsense.
Didn’t I see a joke not long ago?

My daughter was born and I didn’t know what to name her, so I named her a girl.

If you really don't know what name to give it, it may mean that there is something wrong with the variable, or you should learn how to name it.

Duplicate code

It's very basic, even very simple. It's even easy to convert it into a function. The only problem is that I haven't seen those repeated points yet.

You can also give a reason. If you need to make changes later, you will need to make changes in multiple places. Why bother?

function too long

A function should not be too long. This is often misunderstood. A function is just a large piece of code that is put together for easy use.

This statement is not true. Use is one aspect, but the function is too long, making it difficult for people to find the key points. The optimization plan is to split it into small functions and provide an entry function to call these small functions.

Don't worry about the extra overhead of the function, it's part of the plan and prepared for refactoring

Is it annoying to jump back and forth among functions? I don’t know which program doesn’t do this?

If you have difficulty naming, is this the reason?

One thing mentioned is that there is a comment before a line of code. This may be an extraction point. You can even get the name of the function from the comment.

Parameter list too long

Parameters determine the changes of the function. Parameters that are too long are not even recognized by the mother. What will happen if they are passed in or not passed in?

Why do parameters appear? It can’t be all global variables, and getting parameters across scopes will also lead to inaccurate names of parameters in the function body.

My own experience is that you should not exceed three parameters. If you really need more than three, please consider using the object parameter passing method.

fn(1, 2, 3, 4)

fn([1, 2, 3, 4])

// or
fn({
    
    a: 1, b: 2, c: 3, d: 4})

global data

Perhaps here, you can understand it by translating it into a global variable .

For a variable, you only need to ensure that it can be obtained from the place where it is needed. Permissions should be closed, not open. Limit the scope of the variable to an area and use it exclusively.

Even as mentioned before, by displaying the parameters and not by reading across levels

variable data

For data that can be guaranteed not to change, you can declare it as a constant, which is also a way to improve performance.

One method mentioned is that the function returns a value and saves a specific value. This method can ensure that the source data will not be modified. Get a copy and play with it yourself.

function data() {
    
    
  return {
    
     name: "bob", age: 18 }
}

In general, if you can use constants, then use constants

shotgun modifications

Modifications need to be prudent, do not spread the authority everywhere and let it be squandered.

Integrate the required parameters and modification code, and perform modification operations only through this entrance

Talk about versatility

In order to prepare for the future, create something that is not needed now. Sorry, please add it when the time comes. Don’t think about getting it right in one step. This will cause a lot of obstacles in the middle.

Temporary field

Try to convert this field into the return value of a function. Just use the function where needed.

Comment

When you feel the need to write a comment, try refactoring first to make all comments redundant.

Well, it’s good to have comments, but only qualified comments. What I’m most afraid of are the wrong comments.

Maybe some comments are correct, but in the long-term flow, the code is updated and the comments are not, which leads to a bad result.

But we still hope to have a little bit, a little bit

Guess you like

Origin blog.csdn.net/qq_49661519/article/details/124080176