Debugging and testing are two commonly overlooked topics in programming. The purpose of both is to make sure the code you write is working as expected. This article will briefly explain what debugging is and why you should have a debugger ready for action when it’s needed.
What is Debugging?
Let’s first discuss what debugging is. In one sentence, debugging means the inspection of source code and its behavior during runtime.
One important tool used in debugging is the use of breakpoints. A breakpoint can be inserted to any non-empty line of code. If you have inserted a breakpoint to a certain line and start debugging, your program will pause its execution when it reaches that line. When the program is paused, you can inspect all the variables and objects that are relevant in that situation and view their values and properties.
When the debugger reaches a line of code where a breakpoint has been set, you can choose to inspect the line further or jump over that line. If a breakpoint has been set to a function call, you can jump into the function call to see how it works behind the scenes to better understand what the function really does to the variables it has been given as arguments.
Why Debug?
The above explanation likely gave some hints regarding why debugging is useful. First of all, you no longer have to dump the contents of variables using functions like console.log() in JavaScript or die() in PHP. While using these functions generally works, they only give you the values of whatever you pass to them, not the logic how your code ended up assigning those values to the variables.
Additionally, when you debug, you can see all the relevant variables and their values at once, while when using functions like console.log() you have to do it separately for each value you want to inspect. It is also easier to inspect objects and their properties while debugging because you can see all of their properties in a simple tree hierarchy view, depending on the IDE and debugger you are using.
While using a debugger may sound compelling, there is at least one downside. Setting up a debugger for the first time can be a daunting task. However, the good thing is you only have to do it once. A good thing in programming is that you are given multiple choices to achieve your goals. There is often no single right way to do things. If you feel like console.log() is enough for your needs, that is a good choice. However, if you need something more advanced, give debugging a try and see if you like it more.
No comments yet (leave a comment)