Hey guys! Welcome back to a 100 Days of Code. Today
we're going to look at debugging,
the process of removing bugs from your code.
If you manage to get to this point,
you've probably written quite a bit of code already,
and I will bet that you've probably made some mistakes, some typos,
some errors and some bugs. So in this lesson,
I want to talk about some techniques and tips for how to find bugs and how to
get rid of them from your code. Now,
the first documented bug was actually found by this lady, Grace Hopper.
She was probably one of the first programmers and one of the pioneers of the job
that we're undertaking right now. Now in her notes,
you'll find a moth that's been taped to the notebook,
and this was found in a relay that was preventing her code from running
properly. And this was pretty much the first
actual case of a bug that's been found in a computer.
Even though we're not dealing with moths these days anymore,
we're still going to end up finding bits of things in our code that prevent the
code from doing the things that we want it to.
The important thing to remember is don't feel down when you've created a bug
because everyone gets bugs. And when I say gets bugs,
I mean, we create them, right? So once you've gotten over this, next,
I want to talk about some of the tips and techniques that you should follow on
how to quickly be able to identify and remove these bugs.
So the first step is to describe the problem.
If the problem is messy and it's not well understood in your head,
then it's almost impossible to debug it.
So untangled the problem and try to make sense of what's going on. To begin,
go ahead and fork the starting repl for today's code.
Now, the first thing to notice here is that I've written a bunch of code in the
starting repl
and we're going to be uncommenting these blocks one by one and eventually
solving all of the debugging problems.
So the first thing you're going to do is to uncomment the first block of code.
Now in this block of code,
we're going to practice describing the problem to help us solve this debugging
problem. Now if you take a look at this function
you can see that at some point in the function,
we're supposed to print this line out into the console.
But if we go ahead and hit run, you can see that nothing gets printed.
So what's going on here?
I want you to take a look at this function and describe the actual problem.
What is the for-loop doing? When is the function meant to print "You got it"?
What assumptions are you making about the value of i? So pause the video,
read the function and have a go at describing the problem.
All right.
So what we've got here is we've got a function that loops through all the
numbers between 1 and 20,
and then once that number reaches 20, it's supposed to print "You got it".
But the problem is that when i reaches 20,
it doesn't print this line into the console.
So now that we've described the problem and we understand what's going on,
let's see if we can solve it. If we think about this problem,
when i reaches 20, it doesn't print out this line.
There's an assumption in there, right?
The assumption is that i will definitely reach 20.
But if you think back to how the range function works,
the stop or the upper bound is actually omitted.
So when you write range(4),
it actually produces a number from 0 up to 3.
And in our case, when we write (1, 20),
it actually goes from 1 all the way up to 19
but not including 20.
The problem here is that i actually never reaches 20.
So this assumption is completely false. Pause the video
and see if you can fix the code so that this line actually gets printed.
All right. So all we need to do is change this to 21 instead of 20
and now when we hit run, you can see it actually works
and we managed to debug this problem. Always when you come across a problem
in your code,
try to describe it so that you really understand what the issue is,
and then test your assumptions and see which of those assumptions is actually
false.