Let's talk about a very important topic if you want to be a great programmer, and that is called debugging.
You see, nobody writes perfect programs.
It's honestly impossible.
We're always going to make mistakes and programs are constantly being improved.
We constantly improve our code as we find more and more mistakes because programs are full of errors
or what we call bugs or exceptions at runtime when the code is running.
You see, the act of finding and removing these bugs or errors from our code is called debugging.
In a big chunk of our time as programmers is debugging code, whether it's our own code or other people's
code, and figuring out, hey, how come this doesn't work?
And the senior developers are really, really good at debugging.
So let's talk about in this video about some of my recommendations for debugging and what some good
practices are.
Now, keep in mind that you get better at debugging code as you code more and more.
So the best thing you can do is to just keep practicing, keep writing Python code.
So what are some of my tips?
Well, first, you definitely want to use some lending.
And what is lending?
Well, we've talked about it right when we set up our developer environment.
Lending allows us to detect as we code some issues with our code.
For example, if I do num plus four here and I get a red underline and you see here that the Pi Flakes
lender notices before I even run my code that hey, there's an undefined named NUM.
So lending allows us to find these errors before we even run our code.
And we saw that we can use IDs like pi charm that has lending already built in or use sublime text or
Visual Studio code and install packages and extensions like PI lent to actually have lending in our
code.
That is a really, really important tool.
Next, I recommend always using some sort of an ID or.
Ed, because these editors and IDs, especially the ones that are specific for Python like Pi char,
have all these built in tools for us to use, such as auto formatting based on Pep eight.
Remember the style official style guide that the Python community has?
It allows us to detect errors in our code with, let's say, highlighting and formatting of our code.
So it becomes easy for us to detect any errors before we even run the code.
Finally, you want to be able to learn to read errors.
And this is something that I try to practice with you as we've learned, Python.
For example, if I do four plus, let's say gibberish here and I click Run.
Right away.
I see.
Hey, on line six.
There's an error in my main PI file.
There's a type error, and I see here that I'm trying to add int and a string.
So we're able to read errors and understand what they mean.
Maybe I do something like this.
If I click Run.
I get a syntax error.
And a syntax error I know means that.
Hey, Python interpreter is trying to read my code, but what you just wrote is not Python.
I have no idea what you're doing.
Here.
I see that it's end of line while scanning string literal.
So I'm trying to read this string and all of a sudden the program ends like you're supposed to end the
string, remember?
So being able to read these errors is really, really important.
And we've discussed some of them in this course, some of the most common ones like syntax error, where
you're just writing something that Python interpreter doesn't understand.
We have something like name error, right?
Where we use a variable that doesn't exist.
So Python doesn't understand.
Hey, I have no idea what name is.
What's what's the value of name?
We have things like type air.
That is when we mismatch data type and do let's say a string plus four.
We have things like value error.
We have things like key error where we're trying to access a key that doesn't exist or attribute error
where we're trying to access the method of a object and that method doesn't exist.
Now, these errors are something that we've been seeing throughout the course, and we're going to see
a few more.
I would say that there is about 15 to 20 errors that are really, really common that show up 90% of
the time.
And if you remember, you can always go to the Python documentation to learn about them.
But this just comes with practice.
If you see an error that you've never seen before, I recommend going to the documentation and reading
up on it once you see it.
You usually tend to remember when it comes up next.
But let me show you my favorite way to debug code.
And it's called PDB or Python Debugger.
And PDB is a built in module in Python right here.
Again.
Remember, our built in module are part of the standard library that Python comes with and PDB is the
Python debugger for interactive interpreters.
And you can read about it here, but let me show you what it does.
You see.
PDB is extremely useful because it allows us to interact with the code.
So previously when we had a function, let's say we have function ADD and we have num one, num two
and then this function simply returns num one plus num two.
Now before if we wanted to, let's say debug this code and let's say we're adding four plus some gibberish
and it's giving us an error.
The first step that I would take is most likely adding a print in here so I could say print.
Hey, what's what's going on?
Let me see.
Number one and number two and what it's giving me.
And print is a really, really quick way and easy way to debug your code.
I use it all the time, but PDB gives us a little extra boost.
So we can use it like this.
We simply import PDB and instead of using something like a print function, we can simply say, Let
me add a space in here.
We can simply say PDB for Python debugger and then say set trace.
And set trace, I would say is the most useful method that we have on this PDB object that we imported.
You see when I run a set method.
It now gives me an interactive Python debugger that I can now type commands in here and actually test
on my code.
And here's the interesting part right now in this PDB, I'm right inside of line eight.
So my code tried to run, but then it paused as soon as it said PDB set trace.
And now I'm in this world and I can test out what's going on so I can actually say, Hey, num one,
what is it?
Well num one is four.
What about num two.
Well num two is a string and I can be like oh I'm trying to add a string and an int this, this doesn't
work so I can detect errors this way as well.
Now this PDB interactive actually has a ton of useful things.
So if I type in help here.
You see that?
I have commands that I can use in the PDB.
So I have a ton here.
I have exit, which I'm assuming is going to exit me out of this PDB, Python debugger.
But I also have.
Things like.
Let's say list.
Let's see what list does.
So when I type in list here and right now the website is really.
Not showing me too much, but hopefully you can still see list.
And I guess some sort of an output.
I see that I get a list of my code and OC, but let's say I don't know exactly what list does well.
PDB allows us to type, help and then list.
And when we do that, it actually gives us the documentation, just like we have here.
If we go to, let's say, list.
Let's see.
List, list, list, list.
Right here.
It gives me the documentation right in here.
So it says list source for the current file.
All right.
So it just lists the source code that we wrote.
Okay, that's cool.
What else is there?
Well, we also have something like step or se for short and step is really, really cool.
Let's actually use clear to bring this up to the top.
So I'm going to say clear so that it clears all breaks.
Let's do.
Let's make this a little bit bigger so you can see.
There you go.
That's better.
All right.
So now I can actually do something like step and step is going to allow us to go to the next line.
So, again, if we make this a little bit bigger.
Step is going to say, hey, go to line nine.
So let's see what happens.
I'm going to type in step.
Oh.
And I got step twice.
So let's right step.
And look at that.
I go to the next line in my code and it gets me.
Hey, you got a typo?
Error.
The error that we got.
So let's add another line in here.
Let's say that we're trying to add four plus five in here or four multiply by five and assigning assign
it to a variable.
If I stop this and run this again.
I'm now inside.
Over here.
If I type in tea, I get a name.
Air.
Tea is not defined because.
Well, I'm inside the code here on line eight, but we haven't ran line nine yet.
So let's do step.
And now I'm on the next side.
So if I do pt now I get hey t equals to 20.
Awesome.
So step I can step through the code which is really, really useful.
I also have things like continue which lets me continue through the code until I return something and
I continue and exit out of PDB and look at that.
I get the error.
Let's run this again.
Another useful command is a and a actually gives me all the arguments of the current function that we're
in.
So look at that.
I get all the arguments which again is really useful.
There's also the command w.
Which shows me the context of the current line it is executing.
So you can see here where we are, we're in the ad.
Function.
We called it with these arguments, and we're now about to run this line.
And then finally, as we're debugging this.
All right, I think I finally see that we have an error.
We should have given it a number so I can now just exit out of here and simply fix this to five.
So now I can just remove my PDB and everything.
Should be working fine with my code.
Awesome.
So PTB is awesome to step through your code and debug and try different things.
It does require a bit of practice, but after a day of just playing around with PDB, you will become
really, really good at debugging.
So after this video, I recommend that you write PDB and hopefully you have a large enough Python file
that you can just step over and experiment with different commands using help, using list to see what
you have available to you.
Actually, I want to show you one last thing.
So let's say we go back to our code, our PDB set trace.
I click run.
Could I do something like this where I notice that I type A and I have.
All right, my code is working, but could I change num two to now equal some gibberish?
Well, let's see if I do next now.
And let's do next to run this final line.
I get an air.
So I'm also able to change variables in here and write Python code to perhaps test out my functions.
This is now working.
I can test out, hey, what happens if we change this variable to another value?
Again, very, very useful.