How to find bugs in the DotNet Program

Source: Internet
Author: User

 

This article only describes how to find bugs, but lists my usage over the years. net programming process to find some methods of bugs, you are welcome to follow the post, your understatement may be able to lift some of the people's long-standing confusion.

It is common to write programs with bugs, but they have various forms. Some of them can be seen directly, some are hidden, and they can hardly be seen from the appearance, only specific occasions can induce and activate such bugs,
We often heard people talk about how to standardize code writing, pay attention to layers, coupling, number of rows of functions, etc. These good statements can indeed reduce the chance of errors and the time for troubleshooting, however, people are not machines, and errors always occur. It is even more important to pick them out in a timely and effective manner.

Next I will talk about how to release a BUG based on my experiences and frequently encountered bugs.
1. obvious bugs. When running a web page or winForm program, an error occurs on the page or program interface, indicating that a file or line has an error. This type of BUG is easy to find and can be easily cleared. We only need to find the corresponding line according to the error prompt and find the problem.
2. Hidden bugs. During normal running, or ideally running, the program surface has no problems, but the client environment changes or the parameters submitted by the customer are special, because your carelessness did not take this situation into consideration, this kind of BUG was triggered. In fact, it is not difficult to find the client to clear this type of BUG, but it is almost impossible to find the client to induce this BUG. Then you have to reconsider all the extreme situations. programming cannot always be considered as desired. Facts have proved that all extreme conditions must be taken into account when considering parameters. For example, if you want to get an int? The negative, 0, NULL, MaxValue, MinValue, and so on must be considered at the same time. Because you can never expect the client to interact according to your desired state.

Solution:

1. breakpoint method:
  

This method can be used when we cannot determine the specific error location, whether it is obvious or a hidden BUG.

The operation mode is to right-click a line on the VS operation interface, and select "BreakPoint" = "Insert BreakPoint" in the pop-up menu.

Or click the left mouse button in the gray box on the left. A small circle of red solid appears, indicating that you have successfully placed a breakpoint here.

 

For a WEB application, right-click a file in the solution window on the right and select "Set as start page ".

 

Finally, press F5 or select start Debug from the debug menu or directly click the green play key on the interface.

 

When the program runs to the breakpoint you specified, it stops. Then, you can compare the context and check whether various data and parameters meet your expectations. If there is an error, it indicates that a BUG has been generated before the breakpoint. Before that, continue the breakpoint and repeat the DEBUG process. If the breakpoint contains all the data and parameters that meet your expectations, then you can continue with the next breakpoint until you find the wrong line.
2. Minimum system method:
  

Sometimes, in the face of a huge and BUG-prone logic, we are at a loss and don't know where to start. It may take a little time to debug with breakpoints. At this time, we should supplement the minimum system method, comment out the lines of code that are most likely to go wrong, and then compile and debug the code to quickly narrow down the search scope. After all, the final problem points to a method. If the method is standardized, there will be no more than 13 lines of code. Of course, what if I use more than 130 methods? Then I suggest you first optimize the method, and split the method of the bulk header into small blocks before debugging. This suggestion is not only for debugging, but also for future code maintenance and even system upgrades, code reuse, and code porting.
3. Direct value method:
  

Sometimes an error is reported for some method. Maybe you have a LOG to record the error, but you actually only find "object not set to object reference" Through LOG ", I still don't know what is going on, so you need to check all the parameters of this method, and whether you have considered the extreme situation before entering the method. I have already mentioned the extreme situation, if the problem lies in the parameter, it is easy to find all references to the method and add the parameter judgment before the call. If there is no problem with the parameter, you can perform a unit test and write a small piece of code to call this method directly. All parameters first give the ideal value and the extreme value, and believe that the BUG can be reproduced quickly. Here, I would like to say that the method parameters should not be too many, and too many parameters may cause errors. But now you argue: What should I do if I need 20 parameters? In fact, you only need to create a Model class and use all these parameters as its fields. In this way, you can encapsulate all the parameters in the class and then pass the instance to the method, in this way, the code is quite beautiful, and it also reduces the possibility of bugs caused by incorrect writing.
4. Search Method:
  

Sometimes you call an unmanaged assembly, run it, and report an error. Generally, there is no permission or XXX is not registered. The fastest way to solve this problem is to search for answers online. Such errors caused by the environment generally do not happen to you alone. All the search answers are the fastest choice, or you are a very small number of people who take charge of the operating system, it can be solved without searching.


Summary:
1. The BUG is hard to find.
2. compress the number of rows in each method to the ideal range. Generally, 13 rows are selected as the MAX value, which can effectively shorten the BUG search time.
3. The number of method parameters is not too large. It can make the code beautiful, facilitate later maintenance, and reduce the incidence of low-level errors.
4. If you cannot find the answer, remember to search for the answer on the Internet, but the error "You have not set the object to reference" cannot provide a definite answer. Because you are using a NULL value attribute or method, God cannot save you, only self-help.
......
I hope this article will inspire you. More friends will join us to share your debugging experience.

 

From: Active Caterpillar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.