From
http://www.dalianit.com/edu/|78|85|6|52|78|85|86|62|.html article, thank you for your originalist!
Use the prioruated technology to get high quality code faster.
By Bill Wagner
The .NET Framework contains many tools to write the right programs faster and easier. But we have to face this situation: BUGS appears. No matter how simple the program, the programmer may have an error. According to my experience, most programs BUGS appear between programmers: When a programmer writes by another programmer calls. I don't know what is, the caller destroys the assumption that the code is written. Who is the fault? This is not tight, more important is how can you fix it? Here, these techniques will help you find and resolve these issues more quickly before the program is put into use. In the end, these techniques will help you diagnose any problems that appear in use.
Test hypothesis condition test assumptions are the most important way to build the correct program. When you write a function, you should consider and make sure you have done what kind of assumptions for that function. You should ask these questions:
1. What kind of this object must be like this object when this function is called (object initial test, an intrinsic variable value)?
2. How will this object (still # 1, but including the side effect of this function)?
3. What is the parameter of this function (allowing null value, what is the range of input values)?
4. What is the return value?
Once you ask yourself these four questions and make an answer, put the answer in the code. In C #, use the ASSENT method of the System.Diagnostics.debug class:
Public Bool ProcessIctions (Int Numiters) {Debug.Assert (Numiters> 0, "ProcessIctions.", "Itemions Must Be More Than 0); // More Code ...
The code snippet executes the Numiters parameter must be greater than that of zero. If you call ProcessIctions with an invalid parameter, the assert is triggered. At this time, the program stops running and notifying the user to appear. The Assertions is only compiled into the program in the Debug version, so they do not affect the performance in the production situation.
Why use this way? Using this technique ensures that it quickly found that the method of your class is unpredictable. Then, or the caller modifies his code, or requires the modification in your class's behavior (Behavior).
Verify that most functions in a C # program are an instance method on an object. There is a hypothesized assumption for any object of any object. When a public approach is called, you should ensure that those hints have been tested. The conditional compilation characteristics of C # make this easy.
First, write a private function to test the integrity of the object. When you do this, the method is marked "Conditional":
[Conditional ("Debug")] private void imok () {debug.assert (this! = Null, "Testing Object State", "this cannot be null); // more here.}
Then, in each public method, the IMOK method is called:
Public Bool ProcessIctions (Int Numiters) {Imok (); Debug.Assert (Numiters> 0, "ProcessIctions."); orthotelease version, the compiler automatically cancels IMOK transfer.
Why use this way? Using this trick, you can quickly find any case where your object status is invalid.
Use Debug and Trace Output Print Diagnostics Messages to help you make sure your program is wrong. You need to know what happened when triggered an Assert; you also need to know what happened before this. Knowing these best ways is to use your code, so you can easily see what function is called before BUG appears.
When generating debug output, .NET Framework has some new features available. The System.Diagnostic.debug class allows you to format the debug output and easily create different class or level debug output. Here is some of the guidelines I like to use.
First, build a TraceSwitch object for each class in your program:
Public class myclass {private static traceswitch myclasswitch = new traceswitch ("myclassswitch", "conflass the / debug output of myclass);
Then, use Writeif () and WriteLineif () methods to record any information you feel help from tracking your program:
public bool ProcessIterations (int numIters) {WriteLineIf (myClassSwitch.TraceInfo, "Entering ProcessIterations", "CallTrace"); ImOK (); Debug.Assert (numIters> 0, ". ProcessIterations", "Iterations must be more than 0") ;
I prefer to use WRITELINEIF (), which can print an error message as well as the wrong type. The first parameter contains a value for debugging, allowing you to control what level of output.
The use of System.Diagnostics.Trace is exactly the same as DEBUG. Different places is that Debug is only compiled into the Debug version, and the TRACE statement is compiled into the debug and the Release version. Therefore, it should be more careful to use Trace statements. Use the trace statement to help you find BUGS or capture the code in the programming actual combat.
Why use this way? Use these methods to let you know the order of code execution. This helps you determine what action (Actions) before the program is wrong.
Dynamically control the output of these new .NET Framework classes is by editing a profile, you can change the level of the Trace Switch. Built an XML file in the application directory, the name of the file is the same as the name of your program, the extension is ".config". For example, if your program is MyApp.exe, build a myapp.exe.config. You can use this file to set the value of your tracking switch. For example, the following documents:
XML Version = "1.0"?>
Why use this way? By using multiple switches and creates an appropriate config file, you can change the record output, concentrate on those elements you care about.
Set your Listener.NET Framework with a list of Listeners that receives Debug, Assert, and Trace output. By default, your application has a single DefaultTraceListener. This Listener ignores the DEBUG and TRACE output, displaying a dialog box for an ASSERT message. You can add a project to this collection, or remove the project from this collection. Two have been created for you is TextWritertracelistener and EventLogtracelistener. TextWritertraceListener writes the message to a stream, EventLogTraceListener writes the message to an EventLog. EventLog allows you to write debugging and tracking messages of your program to system event logging.
I like to build a debug log file for all programs:
Static void main () {debug.listeners.add (New Textwritetracelistener ("MyLog.log"); // etc.
Why use this method? This trick allows you to control where to use debug and tracking statements.
When you discover BUGS, use these skills that no one uses all of these techniques when you start writing code. In fact, when we strive to find those major BUGS reasons, we usually add these statements. Try the following methods when you fall into the kind of dilemma:
1. When you create a class, you usually build a tracking switch for each class.
2. Usually build a verification function for each class.
3. When you want to diagnose the wrong behavior, add other tracking and debug statements. Confirm that these changes are kept in the code. One of the most common mistakes I found is: When the programmer wants to find BUGS, they add a lot of tracking and debug statements to discover errors. Then, once they find the mistakes they look, they delete these statements.
These tools will help you discover and repair bugs, just see if you don't need them.
About the Author:
Bill wagner is
SRT Solutions founders and consultants, special research .NET development. He is the author of the C # Core Language Little Black Book book. For more than a decade, he has been engaged in software development and teaching. he is
Visual Studio Magazine's columnist and speaking at many meetings about improving software development habits. The scope of the software program he is engaged in is wide, from children to bioinformatics research. contact details:
WWagner@srtsolutions.com.