Debugging Tips to debug any piece of code with my examples

Debugging Tips to debug any piece of code with my examples

Introduction

Debugging your code is as essential as writing the code. Debugging is a most underrated skill that really differentiates lots of people in terms of skills. we all expect our code is gone work flawlessly but regardless of how professional you are or how beginner you are your code never going to perform in the way you want it at least in the initial phase. Sometimes debugging can be frustrating and time-consuming it might take more than a week or month but with the right approach, we can reduce the time and make the process easier. In this blog, I share about debugging and some tips that help you to debug your code more easily and effectively.

What is Debugging?

When you mistakenly add a tablespoon of baking soda to your cake instead of a teaspoon that will ruin your cake by causing bubbles on the cake. Debugging is like finding and fixing mistakes in a recipe just like how a small mistake can ruin your whole dish. Similarly, even a small error in software code can trash and gives an unexpected output.

Debugging is the process of identifying those mistakes, understanding what went wrong, and fixing them. Debugging is the process of finding the source or organization of a bug or problem in your code.

when you're doing some debugging work the role that you're trying to play is to think like a computer, what's the computer working with, and what values is that holding.

Debugging Tips

These debugging tips will help you to debug your code easily and with the right approach.

Mindset change

The new programmers who are learning from tutorials or books and they are falling along with their instructor and they misspelled a variable or something, then the code breaks. We think something is wrong, freak out I did something wrong. If something gets wrong, you have to go from a doer mindset to a doctor mindset.

If you get an issue in your program, you have to become more curious to solve it, you need to slow down. The process of debugging is a very tedious process where you have to go line by line and you try to figure out what's going on. If you try to rush through this process if you're just trying to get rid of this bug as fast as you can then you might take a longer time to solve it. So, you have to be calm, you have to spend a lot of time thinking and analyzing your code.

You need to slow down and switch your mode of thinking something's not working it means you overlooking something the computer never lies, it is okay to get errors but switch your mindset of doing trying to get something done to accessing and trying to figure out to eliminate what it could be so you can find out what the problem actually is.

Example: As a new to programming, I use to get rushed with errors but we need to be calm and concentrate on errors to solve the problem.

Read error messages

As a programmer, errors are your best friends that help to know a detailed message about what is along in the code. Whenever you get an error, make sure to read the error because that not only tells you from where the problem is generated but it actually tells you what the actual issue is sometimes. Even if don't understand the error message you can copy the error message and paste it into google or StackOverflow, it will give a lot of information about other people who've run into that problem or maybe it will help you to figure it out.

Example: As a student, the first language that is taught in college is C. As we use TurboC software, it doesn't show the line numbers, we have to understand where the error is happening by looking at the error message. And reading errors helps me to solve the bugs.

Debugging

The other tip to debug a code is to Understand and use the debugging tools that you have available like print statements in python and console log in javascript and using a debugger tool. Debugger tools have the ability to actually stop your running application to place breakpoints and move line by line. The debugger tool helps to know what the data inside running and helps you to eliminate potential causes and get to the root of the problem.

There are so many IDEs and software available in the market with debugging tools like Visual studio code, and Eclipse and we can use a web browser to find out development errors. But the problem is that beginners and programmers don't know how to use these debugging tools effectively. So, we have to give some time to learning these tools that will help to save our time while debugging.

Double check logic

Double-check your logic means the instruction that you gave to your computer. Most of the time the bug is in the logic of the code. If you are baking a cake and you add a tablespoon of vanilla essence instead of a teaspoon of vanilla, then the whole cake taste may be bad because you are off by two or three teaspoons, same way with your code it can happen if one little change your code can have dramatic results because it just a little bit off. So, read your code carefully.

Double-check your errors, it will prevent 80% of the problem assuming that you did boneheaded

Example: when you have some experience using that language, then mostly the bugs are from the logic side of the code. Firstly, I used to read the logic code briefly if I can't find then I move to see the whole code.

Rubber Duck debugging

Rubber duck debugging is a technique that helps to debug our code. In this technique, the programmer explains the code line-by-line to a rubber duck or a friend, or a person. This will help you to identify and fix the error. As the programmer explains the code to a duck or a person then the programmer actually goes through each line of code and explains what each line is and what it does, with this the programmer can able to identify the error.

Example: when I was building a simple website for our community service project, I was stuck in one place. The thing is I can't able to align a text in the center when the website is responsive. One of my friends come and what is happening and I tell her the problem I explained the code of that part very briefly to her and I identified my error. This technique is simple yet effective.

Reproduce the problem

Reproducing the problem means recreating the issue or error that is occurring in the code. it allows you to identify the cause of the program and develop a solution to fix it. Once you can reproduce the problem then you can identify the root cause of the problem and try to solve it.

Let's take an example, we are working web application and it allows us to upload images but the user is unable to upload the images and it is showing some error message. when the user reported it, the developer tries to reproduce the problem means the developer also tries to upload the image, then the developer examines the code of that bug and it helps us to debug our code quickly.

Divide and Conquer

When we have huge lines of code, it becomes complex to figure out which part of the code causes the error this is where you're gonna try to narrow into one part of the code. To narrow down the code, we can use divide and conquer to cut down the code into two parts and run one part by commenting on another part that helps to know which part is responsible for the error and keep repeating this process until you find out the code that causes the error. And To know the part of the code, We can do the search in that entire code with the term related to the error and check their neighborhood of it.

Ask a friend

when you try to debug your own code, it can be a little bit tricky because your eyes cannot see the code even if they are just in front of you but if any other developer or friend looks at your code he can immediately point out, there is what the mistake it.

To debug with you they have a fresh set of eyes so they help to introduce you to new perspectives or new methods of problem-solving that maybe you hadn't considered before, teams are stronger when everyone can work to fix problems together.

Make one change at a time

when you're trying to fix the bug make changes one step at a time this is because bugs are delicate things, and changing too many things at a time is not gonna help you pinpoint what the issue really was, just change one thing and run the program and see how the bugs changed or if it's fixed.

Commenting things out

Adding comments is always useful. Comments are not only used to explain your code but they will help to debug our code. You can use comments to comment out the code temporarily that you don't want to run the code at that time and run the remaining code to identify which codes are causing errors. Comment out the code instead of removing the code.

Check small changes

This is important to make small changes and check them off

When you are a programmer or beginner, it is important to check the small changes in the code. Writing a bunch of lines of code and then checking the code that will lead to errors and waste your time in finding those bugs. So, write a few lines of code and check it out and know how it's working that helps you to reduce the bugs, if bugs appear you can solve them more quickly.

Calculator, write down a few lines of code and check them out, instead of writing more lines of code and then checking.

Mental Breaks

As programmers need a creative side to solve problems with creative solutions or creatively try to figure out where things are going wrong.

There is a concept of diffuse learning or thinking. You concentrate on the problem for a period of time and take a break get space away from it and let your brain do some background work you come back and you understand it a lot better. Give some rest to your eyes and take a walk or do some exercise or do some other work that helps to refresh your mind and we may come up with another approach to debug the code.

Remember the bug

When you get a complex bug and you solved it. If you think that the bug is helpful that you don't have to repeat it again. Remember that bug and how you solved it or take a note that will help to remember the bug. And you can refer to it when you get the same error or related bug.

Example: We got homework to create a currency converter in java. I code in the mobile and I got some bugs. Then again I code on the college computer and I fix those errors in the computer lab in the college. As the computers in the college updated. When I want to see the code, it is not available on that computer. I try to fix the bug that I get previously (as I have the code in mobile with bugs ), but I can't able to fix it. So, remembers the bugs or take note of them.

Conclusion

Debugging is an essential skill for any programmer. Debugging can be a challenging and time-consuming process, but with the right tips and techniques, you can make it less time-consuming to debug and more effective. Some of the tips that we have discussed in this blog include:

Mindset change: Rather than being rushed with the bugs, try to be calm and identify the bugs.

Read error messages: The error messages give detailed information about what the bug is. So, read the errors and try to debug the code.

Debugging tools: There are lots of IDEs with the debugging tool, we can use that tool to debug our code.

Double-check logic: Check the logic of the code twice, mostly the bugs occur in the logic of the program.

Rubber duck debugging: With this, we can able to identify the bug by explaining the code to a duck or a person briefly.

Reproduce the problem: Try to reproduce the problem that the user got a problem with the application, it will help you to identify the part of the code that causes a bug.

Divide and conquer: Divide the whole code into parts and find the part of the code that produces a bug.

Ask a friend: Take the help of your friend, the bug you might not able to solve, but your friend finds it.

Make one change at a time: Do not solve lots of bugs at the same time, try to focus on one bug and solve it.

Commenting things out: Comments can also be used to solve and debug the code.

Check small changes: Write a few lines of code and check them out.

Mental breaks: Take some rest and do a walk that to refresh your mind.

Remember the bug: Try to remember the bug, it will help you to solve the related bugs.

By following these tips, you can reduce your time to debug, find and fix errors more quickly, and ultimately become a better programmer. Remember, debugging is not just about fixing errors, but also about learning from your mistakes and improving your code. So, take the time to analyze your debugging process and make changes.

I hope you liked my article and that it will help to debug your code.

Connect with me:

Twitter

Linkedin