The principle of high-quality code

Original link: Programming principles: 101 ways to improve code quality

The principle of simplicity

What: Pursue simplicity

From the beginning to the end, the code is written with the simplest logic, so that programming beginners can understand it at a glance. What we should pay attention to when programming is partial integrity, rather than complex overall relevance.

Why: Bugs like to appear in complicated places

Software failures are often concentrated in a certain area, and these areas have a common feature, that is, complexity. If you strive for simplicity when writing code, it is difficult for the code to have problems. However, simple and easy-to-understand code often gives people a feeling of being unprofessional. This is also the reason why experienced programmers like to write sophisticated and sophisticated code. So we must have sufficient concentration to resist this temptation.

Do: Write natural code

Let go of superb skills and insist on writing code with simple logic. Since the faults are concentrated in areas where the code is complex, we only need to keep the code simple enough that there is nowhere to hide the faults. Don't blindly make the code complicated and bloated, but keep the code concise.

Principle of isomorphism

What: Strive for norms

Treat the same things equally and insist on not being special. Treat them equally, for example, the values managed by the same module all use the same unit, and the number of parameters of the public function is uniform.

Why: Different things will be more conspicuous

The same thing in the same form can make different things stand out. Different things tend to produce bug . Following the principle of isomorphism makes it easier for us to sniff out the differences in the code and find out where the problem lies.
The unified code is quite beautiful, and the beautiful things are generally easier to accept, so the unified code has a higher readability.

Do: Write code that conforms to the specification

Reliability and simplicity are indispensable properties of code. When writing code, you must restrain your desire for expression and put norms first.

Symmetry principle

What: Pay attention to symmetry in form

When thinking about a treatment, also think of the treatment that is paired with it. For example, if there 1 , there must be processing for the mark position 0 .

Why: Help people who read the code guess the code behind

Symmetrical code can help readers guess the code behind and improve their understanding of the code. At the same time, symmetry will bring beauty to the code, which also helps others to understand the code.
In addition, taking symmetry into consideration when designing the code can prevent us from thinking about problems. If the conditional branch of the code is a hotbed of failure, then symmetry is the framework for thinking, which can effectively prevent the omission of conditions.

Do: Write symmetrical code

When there is a "condition", we must pay attention to its "anti-condition". Each control condition has a pair of counter-conditions (conditions opposite to the indicated conditions). Attention should be paid to the unity of conditions and counter-conditions to ensure the unity of control conditions.
We also need to consider exceptions and try our best to avoid them. The particularity of exceptions will destroy symmetry and become a hotbed of failure. Too many special circumstances mean that the demand has not been sorted out. At this time, you should re-examine the requirements and try to eliminate exceptions from the code.
Naming should also pay attention to symmetry. It is recommended to use paired words such as set/get , start/stop , begin/ end and push/pop

Hierarchy principle

What: Pay attention to the level

Pay attention to the hierarchical relationships such as the master-slave relationship, the front-to-back relationship, and the bottom-to-the-bottom relationship of things, and organize the relevance of things.
It is very important that different levels perform their duties, and the same kind of processing does not span multiple levels. For example, if the process of acquiring resources is executed, the process of releasing resources must be performed at the same level. For another example, the processing of the flag positions 1 and 0 of mutually exclusive control should be carried out at the same level.

Why: Hierarchy helps to improve the readability of the code

Code with a clear hierarchy can help readers understand the overall structure of the code abstractly. Code readers can read the next level of code according to their own needs and master more detailed information.
This can improve the readability of the code, help programmers express coding intentions, and reduce the probability of occurrence of bug

Do: Write code with an abstract hierarchy

Design the degree of abstraction of each part when writing the code, and build a hierarchical structure. Ensure that all codes in the same level have the same degree of abstraction. In addition, the high-level code should describe the low-level code from an external perspective. Doing so makes the high-level code that calls the low-level code easier to understand.

Linearity principle

What: The processing flow should be as straight as possible

If a function can be realized by a linear combination of multiple functions, its structure will be very simple.
Conversely, behaviors such as controlling code with conditional branches and unorganizedly increasing the number of states can make the code difficult to understand. We need to avoid these behaviors and improve the readability of the code.

Why: straight line processing can improve the readability of the code

Complicated processing flow is a hotbed of failure. Faults often appear in complex conditional statements and loop statements. In addition, goto such as 060803dfb73b3e that cause the flow to jump are also frequent occurrences of failures.
If you can make the processing flow from high-level to low-level, in one go, the readability of the code will be greatly improved. At the same time, maintainability will be improved, and improvements such as adding functions will become easier.
Generally speaking, the top-down processing flow is simple, clear and easy to understand. We should avoid complex and repeated processing procedures.

Do: try not to use conditional branches in the code

Try to reduce the number of conditional branches, and write code that allows code readers to see the entire processing flow linearly.
To this end, we need to take some special processing outside the main processing. Ensure the unity of processing and pay attention to the process of processing. Remember to look down at the code as a whole from time to time, and check if there are any overly complex parts of the code.
In addition, for the parts that have become too complicated after long-term maintenance, we can consider refactoring them. Clear and reliable design is not only good for us, but also convenient for those responsible for maintenance.

The principle of clarity

What: Pay attention to the clarity of logic

The clarity of logic means that logic can clearly prove its correctness. In other words, the code we write must make it clear that there is no problem at a glance. Any unclear parts should be accompanied by explanations.

Why: Eliminate uncertainty

The code is inevitably read over and over again, so the code must remain highly readable. If we pursue high readability when writing code, we will not use clever ways to write code, and the code we write will be very natural. The code is for people to see and is modified by people, so we must write code for people. Eliminating the uncertainty of the code is responsible for your own work, and doing so can also provide convenience to those responsible for subsequent maintenance.

Do: Write logically clear code

We should choose intuitive and easy-to-understand logic. Either remove or comment the part that will cause questions to people reading the code. In addition, we should use terms that anyone can understand immediately and are not ambiguous. Pay special attention to variable names, etc., which must not be meaningless.

Safety principle

What: Pay attention to safety

It means taking the impossible conditions into consideration when writing code. For example, even if a certain if statement must be established, we must consider else statement; even if a certain case statement must be established, we must also consider default statement; even if a variable cannot be empty, we must check the variable Whether it is null .

Why: Prevent failures from developing into major accidents

The service provided by the hardware must be safe, and the same is true for the software. In terms of hardware, such as heaters, in order to prevent dumping and catching fire, heaters are generally equipped with dumping automatic power-off devices. Similarly, various situations need to be considered when designing software to ensure that the software can run safely under all conditions. This approach has positive significance in the continuous operation of services and the prevention of data corruption.

Do: Write safe code

Choose a relatively safe method to design the uncertain part. List all possible operating conditions to ensure that the software runs safely in each case. Understand the requirements and functions, and correctly decompose various situations into the code, which can effectively improve the probability of the safe operation of the software.
For this reason, we must also consider impossible conditions as the object of investigation, design and program them. However, in order to unify the standard, we had better specify which conditions need to be written and which conditions do not need to be written before writing the code.

阅读 3k



10.2k 声望
3.7k 粉丝
0 条评论


10.2k 声望
3.7k 粉丝