As a programmer, writing code requires a lofty spirit to support it. Writing excellent code requires you to have a deep foundation and good coding habits. Before introducing 10 tips for writing good code, let's first discuss what kind of code is good code.
Robustness)
Maintenance and concise (maintainable and simple code)
Efficient)
Short (small)
Reusable)
Testable)
Portable)
In the face of the above goals, we have summarized the following 10 code writing skills, hoping to help you.
1. I have all the leaders of the hundred companies.
In fact, there are many ways to write code. Everyone has their own style, but there are always some common and basic code writing styles among the many styles, such as writing comments for programs and code alignment. Is it a programming specification? Right is the programming specification.
2. Get a good name
Take a good function name and variable name. It is best to name it according to certain rules. Or programming specifications.
3. Ling Bo may not be wrestling
Here I use "lingbo Microstep" to describe the GOTO statement. In general, the GOTO statement makes the program Skip, which is not readable and cannot be optimized. However, in some cases, the GOTO statement can enhance the readability of the program. Just go ahead, not go back.
4. preemptive action, post-launch to people
Defensive coding refers to situations where errors may occur, such as variable initialization. Consider the processing policy when errors occur. Several more threads are required during the test. Some programs run normally in a single city, but problems may occur when multiple threads run concurrently. Some programs run normally in several threads on a single CPU, however, problems may occur when running on multiple CPUs, because a single CPU running thread is only a narrow sense of parallelism. running programs with multiple CPUs is a real parallel operation.
5. Click here to find out what to do.
The error case here refers to the errors that are not easy to pay attention. If the error case is not handled, the program will not encounter errors in most cases, but the program will crash once an exception occurs.
6. Familiar with the swordsman and invincible
Some APIs are described as "Swordsman" because they have been continuously developed and tested by many excellent developers. They are very efficient and easy to understand. I hope you can master them, get familiar with it and use it. Is it like our ulib.
7. fight each other with both hands
The tests here refer to testing your code rather than testing your code by yourself. Because you are the original author of the code and have the deepest understanding of the Code, and no one else can understand it better than you, you can test the boundary conditions well during your own testing, and some situations where the intention is not enough.
8. Active assertions
Assertion is a good debugging tool and method. I hope you can use more assertions, but not all cases can use assertions. In some cases, assertions are not suitable.
9. cannot be careless
When writing code, be careful with some input conditions, such as the input file, TCP sockets, and function parameters, do not think that users who use our APIs know what is correct or what is wrong, that is, they must consider the error handling problem of external interfaces.
10. The highest level, no action to win
This means that you should avoid writing too much code. The more you write, the more chances you will encounter errors. It is better to reuse the interface functions opened by others or directly call others' APIs.
10 tips for writing excellent code