A quick fix for C # program bugs

Source: Internet
Author: User
Tags assert

This article will introduce you to a quick way to fix a bug in a C # program. The. NET Framework contains many tools that can be used to write the correct program faster and easier. But we have to face this situation: the emergence of bugs. No matter how simple the program is, programmers can make mistakes.

In my experience, the bugs of most programs appears in the interface between programmers: When a programmer writes code that is called by another programmer. Somehow, the caller broke the assumptions that were made when the code was written. Whose fault is it? It doesn't matter, but more importantly, how quickly can you fix it? These tips will help you find and solve these problems faster before your program is put into use. In the end, these tips will help you diagnose any problems that actually occur in use.

Test hypothetical conditions

Test assumptions are one of the most important ways to build the right program. When you write a function, you should consider and determine what assumptions you have made about that function. You should ask yourself the following questions:

1. When this function is called, what does this object have to be (initialization of an object, value of an intrinsic variable)?

2. When this function exists, what will this object be (still a #, but include the side effects of the function)?

3. What must be any parameter of the function (allow null values, what is the range of input values)?

4. What does the return value have to be?

Once you have asked yourself these four questions and answered them, put them in the code.

In C #, the assent method of the System.Diagnostics.Debug class is used to represent:

以下是引用片段:
public bool ProcessIterations (int numIters)
   {
   Debug.Assert (numIters > 0,
   "ProcessIterations.",
   "Iterations must be more than 0");
   // More code...

The code fragment executes an assumption that the numiters parameter must be greater than 0. If you invoke processiterations with an invalid parameter, the assert is triggered. At this point, the program stops running and notifies the user of the error. declarations (assertions) are compiled only to programs in the debug version, so they do not affect performance in production.

Why use this method? Use this technique to ensure that the method of your class is quickly found to be used unexpectedly. Then, either the caller modifies his code or asks to modify it in the behavior of your class (behavior).

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.