How to debug any code
Published Feb 14, 2023
Bugs are an inevitable part of writing code and are part of our lives 😄, we deal with them every single time.
Tracking down the cause of the error or bug is what we call debugging.
We have always had the option to either paste our error in the Google search box or if we have some Googling skills, we can figure out a way to ask Google and get the best result.
But, sometimes it is not that you didn’t use the framework correctly or you messed up with the module. It is an internal bug, meaning your code itself is the problem.
There can be two faces to a bug, your program could be:
- throwing an error
- or, it not providing the desired result.
If the program is throwing errors, then it is easier to fix or easier to Google search.
The real issue is when it not providing the desired result.
These are techniques that will help in figuring out why the code is not providing the desired result or where it might be happening.
What printing means is that you are outputting something at a point where you think the error is happening.
These vary a lot based on the programming language and the program, each language has a way to output to the console.
This is the simplest form of debugging and can help a lot with debugging your code.
A debugger is a tool that will allow you to execute your code line by line at your own pace.
Debuggers are built into the most popular ideas, text editors, and browsers. It works with multiple languages including compiled languages and also in the browser.
The way it works is that you set a breakpoint, a line in your code where the execution will be paused. Then you can take over by executing subsequent lines manually.
This allows you to see the changes in your program line by line, and easily helps you to figure out where or what the issue might be.
3. Rubber duck
Rubber duck debugging simply means explaining your code to an object, your pet, or even another human or developer.
The rubber duck is like an umbrella term, which is the object most developers have on their desks they explain their problems or errors.
The duck or the object will not give you the answer or solve your problem. But in the process of saying your problem out loud 🔊 , a solution might start to ring in the head.
The bug you might be having may be a kind that never existed or a problem you cannot craft a question to feed into the Google search box.
These techniques will definitely help you to conquer most of your errors or bugs.