For many development tasks, it's enough to just print out text at various places in your code. However, sometimes that's not enough to get a handle on exactly how your code is functioning and what the problems are. For such situations, you'll want to use a debugger.
It's common to talk of "debugging" code, and sometimes it just means that you're methodically working through the problem and trying different things in an effort to fix the issue or issues.
Debugging can also have a more formal meaning though- it can also mean using a debugger to step through your code.
A debugger is a program, and another part of many IDEs, along with the compiler and linker. There are a few things debuggers offer, including:
To start using a debugger, you will generally set one or more breakpoints.
A breakpoint is a place where the debugger intervenes to prevent the normal execution of your code. Once you've hit a breakpoint, you can progress through your code one line at a time.
To set a breakpoint, place your cursor on the line of code where you want the debugger to pause code execution, and either:
Once you've done that, try running your project, and you'll see it stop suddenly. Visual Studio will also show you the current value of all the variables in the "Locals" tab. For values of complex types, you can expand them.
Once your code has stopped, you can step through it with either F10 or F11. If you hit code that call out to another function, F10 will "step over" the function (running it, but without stepping through the steps), while F11 will "step into" the code, continuing the debugging process with the code in the function.
You can also apply conditions to breakpoints, to make them trigger only conditionally. To do that, mouse over the dot for the breakpoint to which you want to apply a condition, and click on the gear icon ("settings"). From there, you can apply a condition to the breakpoint.
There are three types of conditions you can apply:
A somewhat related concept to breakpoints is the usage of assert to force your code to stop if something goes wrong. To do that, you'll need to make sure that you include the cassert library.
#include <cassert>
Once you've done that, you can add asserts to your code. For example, you might have a function that causes problems if it receives a negative input. One way to deal with that would be to just return early, as in:
void MyFunction(int input)
{
if (input < 0) return;
// the rest of the implementation
}
The above would work, but has the downside of not making it obvious if a bad input is passed into it. By using assert, we can force the code to fail if it gets a negative number. That would look like the following:
void MyFunction(int input)
{
assert(input > 0);
// the rest of the implementation
}
The assert function accepts a conditional statement. If that evaluates to true, your code continues as normal. However, if it evaluates to false, assert will force the code to stop. That might seem like a bad thing, but it's can actually be really helpful to force code to quit if you know that continuing would likely lead to bad results.
Next week is spring break, so there is no actual assignment.
However, I did promise that I would provide an opportunity for extra credit, so here it is- note that this is completely optional
Take the example chatbot from this week's class as a starting point, and make a chatbot of you own.