"C #" #if Debug and how to better and faster debug

Source: Internet
Author: User
Tags assert

First, catch the exception (try/catch/finally)
I don't have to say, we all know what it does to catch all the exceptions that can cause errors in the program, then add your own processing and keep the program running, and if you don't catch the exception, the program will terminate and simply send the error message to the customer.
Therefore, you should catch the exception when doing all the possible wrong actions, as in the following example, to catch the exception that might occur with a database operation.

<summary>
Getting a database connection
</summary>
<param name= "a_strdatabase" > Database name </param>
<param name= "oa_objconnection" > output parameters, NULL database connection </param>
public void getconnection (String a_strdatabase, out SqlConnection oa_objconnection)
{
Oa_objconnection = null;
String strconnstr = "";
Try
{
Strconnstr = "server=" + m_objini.getproperty ("server") + "; uid="
+ m_objini.getproperty ("UID") + ";p wd=" + m_objini.getproperty ("password")
+ ";d atabase=" + a_strdatabase;
Oa_objconnection = new SqlConnection (STRCONNSTR);

Oa_objconnection.open ();

Log it
M_objlog.write ("Database connection OK");
}
catch (SqlException e)
{
Log it
M_objlog.write ("Database connection error", E);

#if DEBUG
Console.WriteLine (E.tostring ());
#endif//debug
Throw (e);
}
}

}//end class

Second, conditional compilation
Java does not provide conditional compilation, this is one of the reasons I think the Java bad, so in writing Java is to write a class to achieve conditional compilation. So, what is conditional compilation? It is convenient to debug if you compile when you meet a certain condition and do not compile when it does not. We often encounter situations in which we want to know the value of a variable in a process or method, the more commonly used method is to output the value of this variable on the page or console, determined whether it is the value you want, but if there is no conditional compilation, but when you release the release version of the need to manually delete these output statements, time-consuming, laborious , and error prone, and if conditional compilation, it is more convenient. Look at the following example:
<summary>
Class
</summary>
private void Initialize ()
{
Try
{
M_objconnmanager = new Connmanager (M_strinifilepath, "./config/newsdata.ini");
Log = new log ("./logs/newserver.log");
}
catch (Exception e)
{

#if DEBUG
Console.WriteLine ("Initialization" + e.message);
#endif//debug
Throw (new Exception ("initialization" + e.message));

}


}

Notice the #if Debug. Its role is to output exception information at the console when debug, so that you know immediately what the error is, and the sentence will not be compiled when it is not debug.

Iii. Assertion (Assert)
Assertions are really a good thing to note, but unfortunately 80% of programmers, especially web programmers, do not use it or even hear it. It's hard to give an assertion the next definition, and if you want to elaborate on its merits, you can write a book. To put it simply, the assertion is to add a judgment in the right place to make sure it's really right (which is a bit awkward, I'll explain in detail below), and its role is to make sure your program works as expected and to help you locate the cause of the error quickly. The mechanism of the assertion is simple, as in C # The assertion method System.Diagnostics.Debug.Assert the definition of whether a condition is set up, if it does not set up to display a message. It seems so simple, can it really play such a big role? Let's look at the example below.

<summary>
Access to M_strid properties
</summary>
public string ID
{
Get
{
return This.m_strid;
}
Set
{
#if DEBUG
Assertion
Debug.Assert (value. Length% 2 = 0, "Category ID length must be even");
#endif

This.m_strid = value;
}
}//end method

This is a very simple method, to access the value of the M_strid member variable, this m_strid is a string member variable that uses the encoding rule to implement a tree structure, just like this: 010213, two bits are an interval, and by its length and coding rules it is easy to get it in the first tier, The ID of its parent node, and so on. Because the two digits are an interval, the length of this string must be an even number.

See Debug.Assert's sentence. Its role is to determine whether the length of the string is even, if not, a dialog box to show that the category ID length must be even. You may say that you can not see what it does, not to judge a value character does not meet the requirements. This program was written by yourself, so you should know that this is an even limit when you assign this M_strid, in general, it should be true, OK, now let's assume that for some reason you forget this limitation and assign a string with an odd length to the variable, And then although there are problems but the program does not complain, continue to run, when it is too far away, this error is revealed, so that the entire program crashes or the final result is incorrect, even if the program error is in the wrong from the real cause of the reason is very far away, or simply do not complain, this is the reason you want to find the wrong is very difficult, It may take hours or days, and if you add an assertion, it will terminate when you run here, telling you the cause of the error, and avoiding the problems that follow and the time and effort you have to pay to correct the problem.

How, now is not a certain understanding of the assertion, and some interest. Try it, slowly you will feel the power of it. The other thing to say is that the assertion is intended to assist DEUBG, rather than for error handling, so it is generally used in conjunction with conditional compilation, only when the use of the assertion of programming, testing, and when the release is the version of the assertion should be removed, because it is to affect efficiency.

Four, logs (log)
It's probably the best sign to distinguish between traditional programmers and web programmers. Most applications log, and almost all Web programs do not log, hehe. In fact, the log is also a particularly useful thing, if not log, it is likely that the system what happened, what happens to you are not clear, especially for a long time, more likely to appear this situation. So, develop good habits and let your program write log.

Of course, in addition to these, there are many things, such as Trace (trace) step debugging, etc., you can look at the data.
Method I said, use is not your problem, hehe.

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.