Debugging is basically De-Bugging 😁
What is Debugging?
- It is hunting and resolving potential or existing problems (bugs) in our code.
- We go through our code thoroughly and the ultimate goal is to have a bug-free program
So Basically :
Analyze code ---> Fix code ---> have a bug-free program
Tools that help us to do so:
1. In-built IDE tools:
- Those are the tools in IDE we use, for example - pycharm, anaconda, InteliJ idea, VS-code
2. Using Python-Tutor:
- It's a great tool made by an MIT student which navigates through the code while showing the scopes, variables, and many other attributes which is an efficient tool for debugging as well as for looking at the code semantically (i.e what is going on on the backend/actions performed by python)
- You can visualize your code using this tool.
3. Using Print statements:
- Using print statements effectively is also a way to debug our program
- You can print the output of every module of code which gives us the idea of what it is returning
- You can use the bisection method here to efficiently find the specific bug
4. Last but not least use your brain 🧠😎
- As you know the brain is the most powerful of them all
Types of ERRORS :
1. Error messages
trying to access beyond the limits of a list ---> IndexError
test = [1,2,3] then test[4])
- trying to convert an inappropriate type ---> TypeError ( int(test) )
- referencing a non-existent variable ---> NameError ( a )
- mixing data types without appropriate coercion ---> TypeError ( '3'/4 )
forgetting to close parenthesis, quotation, etc. ---> SyntaxError
a = len([1,2,3] print(a)
2. Logic Errors
- It's harder to resolve than error messages because most of the time IDE itself detects error messages and even resolves them however logic errors are hard to find and need the third tool (BRAIN) to resolve them.
- Therefore we can perform some brain exercises like taking a break and letting the subconscious do its work
- Rubber ducking: I find it so useful and efficient, i.e explaining your code to someone or something ( Mostly rubber duckies are used as a common example therefore the term is coined as Ruberducking 🦆 )
📌 You can also use some digital alternatives like Gitduck (duckly) for rubber ducking.
Do's & Don'ts
Exceptions & Assertions
- Exceptions are used when an unexpected error is hit
Using
try:
andexcept:
handlers for example:try: num_first = int(input("Enter the first number")) num_second = int(input("Enter second number")) print ( num_first * num_second ) except: print ("Enter a valid input")
This will print the except block ("Enter a valid input") when any other data type is used as input by the user
- This was for all the errors like TypeError, NameError, and all other errors discussed above
- We can also specify the except block as per the error
for example :
and many more.try: num_first = int(input("Enter the first number")) num_second = int(input("Enter second number")) print ( num_first * num_second ) except TypeError: print ("Enter a valid input")
Here is the list of some of the common errors
- SyntaxError: Python can’t parse the program
- NameError: local or global name not found
- AttributeError: attribute reference fails
- TypeError: operand doesn’t have the correct type
- ValueError: operand type okay, but the value is illegal
- IOError: IO system reports malfunction (e.g. file not found)
Assertions
- Assertions work the same as exceptions in a way but unlike exceptions, it stops the execution of code immediately when encountering an error
- Thus makes it easier to locate the source of the bug
- raise exceptions if users supplies bad data input
use assertions to
check types of arguments or values
check that invariants on data structures are met
check constraints on return values
check for violations of constraints on the procedure (e.g. no duplicates in a list)
📌 Encountering a bug and declaring it as a feature is legendary but debugging is what real legends do 😎