Who is this guide for?
This guide is for beginning programmers who care about how their source is made and would like to learn how to debug their source properly. This guide is not for naive members who think they can run a server without any programming knowledge what-so-ever.
Introduction:
So, what is debugging? The formal definition is "Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a computer program or a piece of electronic hardware, thus making it behave as expected." The story behind it is that Grace Hopper (a computer scientist in the 1940s when punch cards were used as input, output, and programming) found a moth stuck in one of the holes of a punch card. Upon removing the bug, the program worked (thus why we call removing problems "debugging").
Debugging by Comparison:
One method of debugging is by comparing expected output with actual output. It's a very simple thing to do, output the actual values in variables as your program executes using print line / write line statements, then compare those values with your expected values.
Debugging in Visual Studio:
A lot of IDEs (Integrated Development Environments), such as Visual Studio, offer a variety of debugging tools. In this guide (since most of the community uses C#), I'll be showing you some of the tools in Visual Studio. First, let's prepare the project for debugging. Run your project in debugging mode. While the project is running, open the Debug menu. Under "Debug > Windows", open the "Locals", "Auto", and "Watch 1" windows. Drag each window to where you'd like them to be in the solution (I drag mine to the right side with my solution explorer).
Now that your project is set up, stop the project and find a line where you would like to start debugging from in your code (you can do this while the project is running as well). I've created a test project to debug, and will be starting in my main function after initializing an array with random values. On the line you wish to start from, right click and select "Breakpoint > Insert Breakpoint". You may also do this by clicking in the gray margin to the left of the line.
Next, start your project (or have the project run the code where the breakpoint will be hit). When the project attempts to run the code where the breakpoint is, it will pause execution on that line and allow you to see what's stored in variables. Open your locals window or hover your mouse over variables to see what's inside!
Keep the locals window open (it will change because we're now going to let it run to the next line). At the very top of the window in your toolbar, you'll see the debug toolbar (shown below).
Hover your mouse over the buttons to see what they do. There are some very useful buttons there. To execute to the next line (called a step), or to step into a function (if the line you're on calls a function), use the "step into" button. To step over a function (if the line you're on calls a function), use "step over". Finally, if you want to step out of a function, use "step out" and the project will run the rest of the function and bring you back to the outside of the function.
Step through each line, one at a time, and notice how your locals change.
Notice the yellow arrow to the left of the line you're on. I'm going to change the code above it, then drag the yellow arrow to the line I want it to jump back to (so I can run the same code again).
Now, sometimes you'll have a lot of local variables to sort through. This can be problematic if you're trying to focus on a group of variables. That's where the autos window and watch window come in. The autos window will focus on values not only in variables, but in assignments. See the example below:
The watch window will only show variables that you want to watch. To add a variable to the watch list, right click on a variable and select "Add Watch". Also, when you hover your mouse over a variable to show the value inside it, you can pin it to the page using the pin icon.
You can also edit values in variables by double clicking on the value in the tooltip (popup value box when you hover over a variable) or in the autos / locals / watch window.
Conclusion:
There's more to debugging, but I don't want to make this too long. Hopefully this has been helpful to some. Sorry for how bad this guide probably is (I haven't proofread it). Good luck.
Edit: Unit testing information...