DebuggingFeb

DebuggingFeb

Best way to understand the code

ยท

7 min read

Overview

In this article, You are going to read about my first debugging experience. How I first encounter this. I heard the term debug a lot in memes and trolls when I was very new to development. I thought debugging is required for the senior developers as they code a lot in that huge code they use debugging tools to identify the bugs. As a beginner, I barely code almost 20-30 lines of code. But all my assumptions are burned out while going through the same thing I will discuss in this article.

What is debugging?

My way of understanding debugging!

Let's take an example you have something wrong with your code. Let's even say you place the "+" operator instead of "-" in the particular code. As it is a too small mistake there are a lot of chances you overlook when you again look into the code. consequently, you end up with a mess.

Till now I thought every programming IDE has debug option and by using that we can debug the code. But after reading the DebugginFeb article on hashnode I found there are so many debugging tools out there that are used for debugging the code only.

I am not at the level of a developer to uses debugging tools to debug my code. But I know debugging and I have a hell lot of experience with this.

My First debugger

Do you believe my first debugging tool is just a print function in the python code? Even though it seems simple it was my life savior at that time. I heard somewhere that if your code not working that you are supposed to work. Then just place a print statement on every line of your code.

Literally, it makes a lot of sense. I even use this technique if I encounter any problem in the iteration statements like for and while loops.

Let's understand the print statement's importance with the help of an example.

For that, I take a simple example of finding out the factorial of a number. There are so many ways to implement but for better understanding, I use the function.

def factorial(n):
        if n == 1:
            return
    return n * factorial(n-1)
print(factorial(5))
output:
120

We all know that factorial is nothing but continuous number multiplication by decreasing the number by 1 until we reach the number 1.

5 factorial -> 5*4*3*2*1 = 120

We may even start with the number 1 it self until the given number

5 factorial -> 1*2*3*4*5 = 120

But when I came across this I found something happening inside the function.

The mystery revealed

As I was from a non-CS background even though I watched a few YT videos on the call stack it not helped me much.

But instead, use just print statements inside the function. I got some clarification about the things but not actually.

def factorial(n):
        if n == 0:
            return 1
    print(n) #This print statement is the sevier
    return n * factorial(n-1)
print(factorial(5))
output:
5---|
4   |
3   | ===> 5*4*3*2*1
2   |
1---|
120

From the above code with a little bit of change, I got a reasonable sense of clarity than the first.

Because of just that print statement, we are literally able to print the exact factorial format.

This is my first debugging experience. After I figure out this I was on cloud nine. I feel like a hacker/developer. Never mind just kidding.

This is where I lost my sense.

As I mentioned above I always ignore the stack call when even I encounter it. But one day I realized the importance of the stack calls working when came across a few DSA concepts.

Like Recursion, Linked List, and sorting algorithms.

Like everyone I started learning recursion by printing consecutive numbers in ascending and descending order.

Just by one recursive statement and one base condition, we are getting the result. But I am In front of the monitor wondering what the hell is going on.

I decided to understand the internal process for a lot of people's suggested that first I need to understand the stack works.

Even though I watch numerous videos on the recursion not get clear. however, I got a little idea about that.

To get an even more clear idea I started experimenting.

Experiment leads to grip.

The normal recursion code looks like this for printing successive numbers in either order.

def fun(n):
    if n == 1:
        return 
    print(n)
    fun(n-1)
fun(5)
5
4
3
2

The code is running super fine and I understand the flow of code with the help of a debugger.

But in the middle, I got an idea of what if I call the function more than once in the body of the recursion. like this๐Ÿ‘‡

def fun(n):
    if n == 1:
        return 
    print(n)
    fun(n-1)
    print(n)
    fun(n-1)    
fun(3)
3
2
2
3

I will not stop here and went on to add many statements and recursion calls inside the function. I ended up by confusion.

To get more clarity I reached so many people they explained their level best.

But one thing I observed in recursion is I am understanding what they explaining to me but not getting enough clarity when I try out the things on my own.

I just told the same thing to them. They replied to me recursion itself has this problem if you went in-depth understanding you will end with confusion.

Even though I never accept their dialogues I went on searching for someone who makes me understand this concept clearly.

In this way, I found one person sudha who is doing DSA on the daily basis. We schedule an online meeting to discuss this topic. Even though I won't work out.

Meanwhile, I reached out to so people about this, but they didn't interest in the meeting.

But after these many attempts, I ended up with my senior brother he said OK to meet up to explain the recursion.

At the first attempt, I didn't understand but he went on explaining until I said "yes things are making sense".

The problem

In this process, I found that to understand recursion first we first need to understand the stack call working. But I was somewhat familiar with stack calls but for one recursion call it was OK. However, more than one calls leading uncertainty.

This is where I had I utilized debug pointer to build a good grip on the recursion thing.

Super hardware tools.

The super tools that help me to debug the code are pen and paper.

Even though we are seeing what happens inside the stack. It always raises confusion if we didn't use our hands while debugging.

I observed a lot of differences when I start debugging with pen and paper.

Explaining how the code working is even more fun.๐Ÿ˜…

The ultimate guide COMMENTING.

It's not only with recursion when I get stuck with any problem I literally wrote my approach and start begging with the help of comments.

Let's understand the importance of commenting while debugging with the help of Armstrong number finder code.

n = int(input("Enter the number: ")) #153 as an input
int_str = str(n) #converts the int into string
res = 0 # we are storing the result here 
temp = n # we are storing the initial value in this variable
while n > 0:# 1)153>0->True 2)15>0->T 3)1 > 0->T 4) 0>0->False outof the loop
    rem = n%10 # 1) 153%10 =3 2) 15%10 = 5 3)1%10 = 1
    res +=rem**len(int_str)#1)0 + 3 **3= 27|2)27 + 5**3= 152|3) 152 + 1= 153
    n = n //10 # 1)153//10 = 15 2)15//10 = 1 3) 1//10 =0
if temp == res: 153 == 153(True)
    print("Armstrong") #prints as Armstrong
else:
    print("Not an Armstrong")

This is also one of my favorite techniques of all time. Yes takes time but in programming, rushing gives instant happiness building intuition makes you a pro.

This is my way of debugging the code.

Conclusion

To all my fellow coders and beginners after spending that much amount of time on recursion I found that there is nothing like magic in programming all we need is to spend time. One understands the topic in one day one in a week and one can't understand even in one month also, don't get down DO search for the person/resource which suits for you.

Happy coding and Happy debugging!!

Did you find this article valuable?

Support Malavi Pande by becoming a sponsor. Any amount is appreciated!

ย