Thursday, August 18, 2011

Debugging and Trouble Shooting

Overview


There are three major types of errors that can occur in your program: First there are compile errors. These occur before you ever run the program. Typically they consist of mispelled key words or variables, missing semi-colons and curly braces. These must be corrected before you can run the program.

Next there are Run time errors. These are errors that occur when the program is running. Typically they consist of Data type conflicts, or objects that are called before they are initialized. These cause the code to break and go into debug mode.

The third kind of error is a logical error. Logical errors are errors in the logic of the program. They usually don't cause the program to fail or crash. Rather they cause the program to return the wrong answer or no answer at all. These are the hardest errors to track down and fix.



Compile Errors


The key with compile errors is to use the Visual Studio environnment.

Use the intellisense. The intellisense will show you all the objects defined by your program. It will show you all the relevant methods and properties. Using it can help cut down on mispellings and errors in capitalization.

Read the error messages. Actually read them. They don't always tell you exactly where the problem is, but they get you close.

Always work from the first error on down. Often fixing the first error will clear up most or all of the remaining errors. Errors tend to cascade through the program. One causes another which causes another, etc. If you work from the bottom of the list you will get frustrated, because you usually won't find anything wrong.

Pay attention to the color coding in the coding environment itself. A keyword should be blue--if it is not it may mean you mistyped it. A red underline signifies an error. A green underline is just a comment or possibly a warning.


Run time errors


Most runtime errors are caused by user input, Users don't always enter what we expect, so the program should be able to handle the unexpected input. Ideally, test all use input to see if it is in the correct form. Use TryParse() instead of Parse().

The other common cause of Runtime errors are objects that are not initialized properly. Make sure that you instantiate an object (make it new) before you call it.


Logical Errors


The key to finding logical errors is testing. Try the program with known values and a known output. If the output varies on the input, test a complete range of inputs to see if the proper outputs are returned.

One strategy is to put temporary outputs in your program that print out the state of certain variables at a given point in the process. You can check these against what the variable should be.

You can also use Visual Studios debugger. You can place breaks in the code and step through the part in question line by line, checking the values of the variables moment to moment.


General Suggestions


When writing a program, do one thing and then test it. If it works then when you add the next thing and the program fails you know it was in the new part.

The same goes when debugging and trouble shooting. Make one change and then test it. Never make a bunch of changes willy nilly. It makes it almost impossible to tell what is working and what isn't.

If you go over the code again and again and can't find a mistake in the logic, chances are good there isn't one. Look for syntax aand spelling errors instead. You often don't see these when looking at the logic.

If possible, have someone else look it over. Another person can often see what you don't.

If you spend hours on a thing and just can't get it to work. Walk away. Do something else entirely for a while. Often when you come back with fresh eyes, the problem that you were missing becomes obvious.

No comments:

Post a Comment