Preface
Have you ever felt that there were ghosts in the world when you tune a certain piece of code?
Have you ever had a problem in regulating the API, you always feel that it is a problem in calling third-party interfaces or the documentation is incorrect?
Have you ever felt that the source of the problem was the wrong way to use it?
Have you always felt that the documentation or environment does not match when installing a service?
Believe in the process and method and never be misled by the results.........
Overview
The modular code is often similar to the investigation of a case, but the importance of the result is different. The police investigate the case for the people to be safe, while our modular code is for the stability of the system. In this way, we should not wrongly accuse any piece of code and program, so as not to be punished unreasonably.
The following process methods come from personal summary. From a personal perspective, some of the methods of previous generations have been accumulated through long-term experience, and of course they are highly referenced and theoretical. As personal methods, they may be more suitable for DS like us.
Test Method
Code procedural mode
The first thing to pay attention to when coding mode is the process. You must clarify the idea of the final result, that is, the process of committing the crime, and follow up step by step in the crime process to obtain the result of the crime. During the analysis of the crime process, every doubt must be marked (that is, the log information mentioned in the code). After such an analysis process, black box test is performed, input is added, and the results are verified. Finally, verify your judgment based on the markings of each step to find the reason.
The above solution is a procedural mode. The advantages of this method are self-evident. You can directly analyze the entire process through a test, but this method is time-consuming and it is okay to clarify your own code logic, but it is difficult to understand other people's logic code.
Unit test mode
The basic purpose of unit testing is to ensure the normal operation of a function, class or functional module, including testing and verification of its abnormal situations. As programmers, the most favorite verification method is "piling" (the meaning of pile driving is to provide fake default data). This method is very convenient to adjust, but one disadvantage is that it cannot be used again, because after our verification is normal, many developers will comment or delete it. Therefore, if we complete development in the development environment, but we hope that when testing the environment verification, we must rewrite another pile driving logic. Then, this way, it will be even more troublesome when we are on the Internet. Since there are so many inconveniences, you can try the following methods.
Add a unit test class. This class needs to control its permissions and can only be executed through the background login or the command line. The function of this class is to detect the key logic of the system and make corresponding test output results. You must believe that all interface classes can be tested through unit test classes. Many times programmers are questioning whether we should do this? In fact, we really need to do it. After all, many tests are now done in black box tests.
This modular method is suitable during the development process and can ensure that our current network code runs normally after it is released. I hope that everyone will also transfer this process to the development stage when planning the development time.
Quick positioning method
Are the first two so complicated processes too ideal? My code is only 100 lines, and the system is not complicated. If this is the case, then perform positioning analysis quickly. Many times I encounter it
1. The input is normal, the output is abnormal;
2. The input is normal, the logic is abnormal, and the output is abnormal;
3. The input is abnormal, the logic is normal, and the output is normal;
4. Input exception, logic exception, output none.
During my personal development process, I often encounter some type of problems above. For example, during the development process of Node.js, I encountered string.length and told that there is no length method for string. At that time, I was puzzled and asked myself why other strings have length methods, and why there is no such method? Many students probably know that the problem is that this string is not a string at all, but just that you have idealized it as a string yourself, which means that there is a problem with what you input. Then the best way to locate this problem is to print the input and print the output.
Maybe other programs are not that simple, but the most basic thing is to make input and output judgments on functions that encounter exceptions in the main function, so that they can quickly locate.
Remember: Don’t take it out of context and be self-righteous.
The above methods and procedures are only summarized based on PHP or Node.js. There may be similarities or differences in C & C++. If you don’t like it, please cherish it.