javascriptThe column introduces tips to improve the quality of article code
Recommended (free) :javascript(Video)
Introduction
Mainly introduces the following points:
Refining function
Merge duplicate conditional fragments
Refining conditional branch statements into functions
Use loops appropriately
Let the function exit early instead of nested conditional branches
Pass object parameters instead of too long parameter lists
Use less ternary operators
Use chain calls rationally
Decompose large classes
This article will be continuously updated, please feel free to add any deficiencies in the comment area.
1. Refining functions
Benefits:
Avoid overly large functions.
Independent functions help code reuse.
Independent functions are easier to overwrite.
If the independent function has a good name, it itself plays the role of a comment.
Semantic implementation of multiple separate sections of logic in different functions can make the code logic clear and clearly see what each step is doing.
Code example:
Realize obtaining data, then operate dom to display data, and finally add events
Before function refining
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
After function refining
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
2. Merge duplicate conditional fragments
If there are some conditional branch statements in a function body, and there are some repeated conditional statements scattered inside these conditional branch statements code, then it is necessary to merge and remove duplicates.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
3. Refining conditional branch statements into functions
Complex conditional branch statements are an important reason why the program is difficult to read and understand, and can easily lead to a huge function. Sometimes conditional branch statements can be refined into semantic functions to make the code more intuitive and logically clear.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
4. Reasonable use of loops
If multiple pieces of code are actually responsible for some repetitive work, they can be replaced by loops to reduce the amount of code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
5. Let the function exit early instead of nested conditional branches
Let the function return early with multiple exits and replace nested conditional branches.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
6. Pass object parameters instead of too long parameter lists
If the function parameters are too long, the risk of errors increases. It is important to ensure that the order of passing is correct. The trouble is that the readability of the code will also become worse. Try to ensure that the parameters of the function are not too long. If multiple parameters must be passed, it is recommended to use objects instead.
Generally speaking, it is best not to have more than 3 function parameters
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
7. Use less ternary operators
Ternary operator performance High, less code.
But the ternary operator should not be abused. We should use it in simple logic branches and avoid using it in complex logic branches.
1 2 3 4 |
|
8. Reasonable use of chain calls
Advantages:
Chain calls are simple to use and require less code.
Disadvantages:
The disadvantage of chain calls is that it is inconvenient to debug. If we know that there is an error in a chain, we must first disassemble the chain before adding some Debug the log or add breakpoints to locate where the error occurs.
If the structure of the chain is relatively stable and not easy to be modified later, the chain type can be used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
9. Decomposing large classes
The decomposition of large classes is very similar to the refining of functions. If a class is too large, the logic will be unclear and difficult to understand and maintain.
Reasonable decomposition of major categories can make the logic of the class clear, and sub-modules can be easily reused.
10. Use bitwise operators
The performance of programming languages for calculating multiplication and division is not high, but in some cases, using bitwise operators can improve the performance of operations such as multiplication and division.
1 2 3 |
|
The above is the detailed content of Master JavaScript tips to help you improve code quality. For more information, please follow other related articles on the PHP Chinese website!