How to improve the quality of your code

The capabilities of people vary widely, so the quality of the code written must be different. Some people may need 100 lines to write a small logic, while others may only need 10 lines. There will always be bugs in the code, and there is no best but better in this regard. Modularity and object orientation are ways to achieve efficient error-free code. Efficient, error-free code requires constant iteration of thought and practice. How to make the code efficient and error-free, and what are the ways to improve the quality of the code? What experience and skills do you have?

1. Code quality
 
Software is a product delivered to users and experienced by users; code is a correct and detailed description of the software, so the quality of the code is related to the quality of the software product. Although software quality is not equal to code quality, defects in code can seriously affect the quality of software products. Therefore, the investment in improving code quality is worthwhile.
 
 
Second, the quality of software products can usually be measured from the following six aspects
 
 
Functionality, that is, whether the software meets the customer's business requirements;
 
Usability, which is a measure of how much effort a user needs to make to use the software;
 
Reliability, that is, whether the software can always be in a stable state to meet availability;
 
Efficiency, which is to measure how much physical resources the software needs to run normally;
 
Maintainability, which is a measure of how much effort is required to make adjustments to already-completed software;
 
Portability, which is a measure of whether the software can be easily deployed to different operating environments;
 
 
3. Specific experience in improving code quality
 
 
1. Never copy code
 
Avoid duplicate code at any cost. If a commonly used piece of code appears in several different places in the program, refactor it to put it in a function of its own. Duplicate code can cause confusion among your colleagues when reading your code. If the duplicate code is modified in one place and forgotten to be modified in another place, there will be bugs everywhere, and it will also make your code bloated.
 
2. Test your finished code
 
You know what your code can do, and you try it, and it works, but you actually need to validate it well. Analyze all possible edge cases and test that it works as expected under all possible conditions. If there are parameters, pass some value outside the expected range. Pass a null value. If possible, show colleagues your code and ask if they can break it. Unit testing is the usual way to do this.
 
3. Code Review
 
Before committing your code, sit down with a colleague and explain to him what changes you made. Often, by doing this, you'll be able to spot bugs in your code without needing a word from a colleague. This is much more effective than reviewing your own code yourself.
 
4. Write self-explanatory code
 
Undoubtedly, comments are an important part of programming, but self-explanatory code is better because it allows you to understand the code when you look at it. Function names and variable names should be selected carefully. When a good variable/method name is placed in the language semantic environment, people who do not understand programming can understand it.
 
5. Don’t use pure numbers
 
It's a bad habit to embed numbers directly into code because there's no way to tell what they represent. Worse when there is duplication - the same number appears in multiple places in the code. If only one is modified and the others are forgotten. This leads to bugs. Be sure to use a named constant to represent the number you want to express, even if it occurs only once in the code.
 
6. Don't do manual labor
 
Humans always like to make mistakes when doing a sequence of actions. If you're doing deployment and it's not a one-step process, you're doing something wrong. Try to automate the work as much as possible to reduce human error. This is especially important when doing heavy-duty tasks.
 
 
7. Don't try to speed up the code, it may be more effective to find a more efficient algorithm.
 
8. The code must be done right first, and it is fast. Make it reliable first, then make it faster. Get the code clean first, then make it fast
 
9. When a function is found to have the following characteristics, the extraction function needs to be considered
 
(1), too long
 
(2) The number of nesting levels is too deep.
 
(3), natural block, need to use comments to describe the program block
 
(4) The judgment conditions are too complicated
 
(5) Some judgment branches of the function are constantly changing
 
(6), the parameters are too complex
 
(7), logical repetition
 
10. Local variables should be single-purpose
 
11. Programmers should make clean code style a habit, always be aware of the importance of clean code and constantly improve refactoring skills
 
12. About Notes
 
(1) If it can be described by a short function, use a sub-function instead of the annotation itself.
 
(2) Make sure that the intent expressed by the comment and the code is consistent, otherwise the meaning of the comment will be lost.
 
(3), write comments in important places, do not fill the sky with comments, the simple function of repeating code is meaningless. Make every comment count. Don't over-comment.
 
 
13. About when to rewrite code
 
The development team should set aside 20% of the time to maintain the refactoring of the original system. The remaining time is used to develop new features.
 
Whenever possible, incremental changes are made to the parts to be refactored, so that users can feel the improvement of the product, even if the working time is extended.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326309515&siteId=291194637
Recommended