- Let's look in this video at some
debugging techniques.
Now that you learned to code,
you learn some advance features,
you have some experience.
It is time to also pause
and see how can you find out
if there's errors in your code,
and if you know there's errors,
how can you isolate them.
There are several techniques
that you will have to interpret and adapt
to the way your program style is,
your problem solving style is,
other criterias in other tools and experience.
A simple thing to look at
is always verify values.
If you know that a value is being passed on to a function,
before you do anything inside the function,
tell the function to print the value
so you know for sure that the values
that are being received are the correct ones.
This goes through for initialized variables,
function parameters, and array boundaries.
When you are trying to access an element in the array,
and using an index,
make sure that index is not less
than the lower boundary of the array,
and it's not higher than the upper boundary of the array.
Other techniques for debugging are,
set some smart mechanisms.
I will show you shortly how to do
something like that.
Activate on demand so that you can
set things in place that works when you need them,
and don't do anything when you don't.
Some modern integrated development environments
include debugging tools,
however, as a beginning programmer,
I advise that you build your own
even primitive debugging techniques,
because it will make you explore the code,
you will understand not just how codes
should be written and what it does,
but you will understand yourself,
and you will be able to improve yourself.
Finally, some tradeoff between manual or system tools.
Manual tools are more tedious
because you have to do everything from scratch.
System tools are not necessarily as efficient
and they don't necessarily do exactly
what you want them to do,
but they are ready made,
and you don't have to spend time developing them.
Let's look in the code
and see a system
that I implemented for you,
and you can look at it as just a suggestion.
Before I'm gonna show you,
I just want to run the code,
so you can see what it does.
It does nothing,
I created some functions,
they simply say it's working,
it's not working.
So you can see you have a Function1,
a Function2 and a Function3,
so someFunction1 says its executing,
still doing something, keeps going.
Function2 is just executing, three is executing,
busy looping, still doing something, and so on.
Let's say I want to know more than that.
If you look at someFunction1,
lines 41 through 43 is where those messages are coming from.
someFunction1 is executing and so on.
So basically what these functions do,
they just print some text,
I created a set of functions here that do nothing
except they help me illustrate to you
the concept of debugging.
Let's focus for now on line 38 and 39.
First, line 39.
printf something in someFunction1,
received a equals an integer,
b equals an integer,
and those are "a"s and "b"s,
the values that have been received by someFunction.
So what I'm doing here in line 39,
I am printing the values
of the two parameters "a"s and "b"s.
Notice line 38.
if(debugFlag), in other words,
if debugFlag equals true,
only then you printf this.
And notice another thing, on line 39,
something called DEBUG_ALERT.
Now it's time to look at our macro.
Let's look at a few things here.
First I have three constants,
DEBUG_ON, which is one,
and DEBUG_OFF, which is zero.
I could have used ones and zero,
but the text is so much more readable
when I use those terms.
The other one, it's a DEBUG_ALERT.
I will show you how I can insert that alert as needed.
And finally, I have a debugFlag,
which is now set to off, meaning zero.
I could have said debugFlag equals zero,
or set it equals one, this is more elegant,
and more informative to somebody
that looks at this code for the first time.
Here's my prototype functions,
that one receives two ints,
the other two floats, the other an int and a float,
and this is really non important.
What's important is what we do with this debugging tool.
So here's a little trick.
So notice right now the functions just print
whatever they do,
but I want to verify what they do.
I'm going to set this debug off flag to on.
All I'm saying is, now debugFlag
equals to one instead of zero.
Let's run the code and see what happens,
and then I'll tell you how I did it.
First you can see there's considerably more input
into the function, and some additional texts
are putted back.
There's an arrow that says
In someFunction, received a=3 and b=5.
And now, in someFunction2,
we received a=2.000, apparently it's a float,
someFunction2 is executed,
then we have another message that says
it's again in someFunction2,
with m=4 at line 57 now.
I am also printing on which line, 53 and so on.
So I have enabled that through a simple variable,
I can put this back to off,
and run the code.
And there's no debug information.
So let's see how this works.
I set debugFlag to off right now.
In each one of those function,
before I just print the feedback of what's happening
inside the function, I put an if for debugFlag.
If debugFlag is turned to on,
then I'm printing whatever there is
line 39 says to print.
If not, I'm just ignoring it.
In addition to that,
I have some functionality that's been placed
in what the function does,
I'm computing some things,
I am working on some formulas.
But here's another interesting thing,
is that DEBUG_ALERT, m Line,
so if I hover over,
notice that is that arrow.
So I essentially created a value,
DEBUG_ALERT, that stores that little arrow,
so whenever I want to display it
and draw attention to the debugger,
here's the arrow,
the other one which we saw earlier,
two videos ago, is line.
Line is a known processor directive that says
print the line that you are currently at.
Very handy if you want to print
and find information that's deep inside the computer
and you don't know even where the file is.
That's a perfect usage of that line.
And this is pretty much it.
Every function does something extra
to its core, but other than that,
it pretty much straightforward
turning a flag on, off.
If it's on, you print a text message,
otherwise you don't.