"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "
Writing the code itself is not a very complex thing. According to certain logic, with certain IDE tools, the sleeves will be ready to dry up. But there is no denying that the quality of any development code is different. Although some students write code for a long time, but often cram, to structure no structure, logic is not logical, not to mention the expansion, testing and performance analysis. So, how can individuals write code that is quality?
Structural Analysis and Design
When it comes to writing code, everyone likes to think on the same side. In my personal experience, this is not a very good approach. To write the quality can also be code, must be logically withstood the test, whether it is the state machine or the exception of the consideration must be complete. Because when you write code, your energies are often scattered, and there is no way to think about each situation at all. So, before the code is not as good as the data structure, analyze the logic and process of the code, at least do hand to, pen to can. Some of the relevant work can be done during this period,
1. Design clear code of the process and state machine
2. Mark the process and icon with Markdown
3. Analyze the deployment environment for future code
Read it again before compiling the code
After writing a good code, many students like to run immediately, anxious to see the effect immediately. However, the situation of one step is seldom occurring. More or less, your code will have problems like this, and instead of discovering these problems in the future, you might as well read them a few times and tell yourself. If you feel embarrassed, you can change to the form of meditation, buy a small pendant placed next to the computer can also. After reading a few times, general compilation errors can be found by you, or the logic of a very simple error will not escape your discernment. By doing this, you can start compiling and debugging.
Learn to check your code with Pclint and other coding tools
General compiler tools will give an alarm for unreasonable code, but this kind of alarm is very weak. Therefore, we will also use third-party tools to help us detect what risks are present in the code. The more you use on Windows is the more you use splint on Pclint,linux.
Learn Unit Testing
Each programming language has its own programming framework, C has cunit,cpp CPP Unit,java has junit. The purpose of writing unit tests is to test the function for the various inputs, and there are no exceptions to the situation. With the test case, in fact, our confidence in code will be more and more strong, or every time we release the software, in fact, we are frightened. Simple black-Box testing is no way to cover all cases from the coverage, so in addition to the function of dump functions, the matching unit test is very necessary.
Detecting memory leaks
For the general upper software, the most dealing with is memory, device space It also can not access, interrupts can not be done, the exception can not be handled. All resources need to be applied to the OS via Syscall, which is necessary to detect a memory leak. A typical client program might not look that heavy on a memory leak, but the server does not, after all, need to run for a long time. Fortunately, there are ready-made tools to help us detect the relevant situation of memory leaks, Linux used more is the valgrind. Students who are not sure can go and have a good try.
Performance analysis
According to my personal experience, the performance analysis of the software is actually very hurtful, but it is also a rule to follow. In general, for the app, the most time-consuming place is memory copy,lock,database, socket these aspects. Of course, if all these aspects can be done, it may be possible to analyze and optimize from the perspective of the business itself, which often has a good harvest. Fortunately, there are a lot of tools to help us analyze the bottleneck of the software is where, Windows used more is Vtune,linux is gprof.
Automated Integration Testing Tools
Automated integration testing tools like Jenkins are more likely to be in the business. If you write a large software, you can also use this method. If it's just thousands of lines of small code, write a bash script that strings all the actions you use as a script, and it works fine.
Constant refactoring
Most of the time, the code needs to be constantly refactored, just as long as the above logic is constantly iterative, of course, the test case also developed to play its role.
In short, the basic process should be this way,
Created with Rapha?l 2.1.0 Start Design Code static Check Unit Test Dynamic Test Jenkins End
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Random record (How to develop the quality of personal code)