Debugging fundamentals

As we mentioned previously, debugging typically comes after testing, when errors and bugs are detected. The general goal of debugging is to identify the specific causes of those bugs and errors that are detected in testing processes, and, from there, adjust the program accordingly.

More specifically, in a debugging process, a developer watches the interactions between, and the changes taking place in, the variables of their program. By seeing how each different function, method, or even line of code alters the value and functionality of the variables, the developer can narrow down the potential causes for the bugs and errors in the program. To that end, there are a number of common debugging methods that programmers utilize in Python:

  • Print debugging: This is the simplest yet most intuitive debugging method. In print debugging, we simply add in print statements between commands in our programs to display the values of the variables that we think are causing the bugs. By looping through the printed values of the variables and how they change over time, we can visually see the effects of each different line of code on the variables under consideration.
  • Logging: In the most general sense, logging is equivalent to the process of keeping track of various events that take place during the execution of the program we are trying to debug. In terms of the output logs that are produced in this process, it is quite similar to print debugging, but programmers typically store the output in log files that can be viewed later on. Logging is quite common among server administrators, who often have to handle server crashes and other site-reliability issues.
  • Tracing: In the process of tracing, low-level machine language details regarding the execution of the program to be debugged are tracked. The results that are produced in this process are fairly useful in terms of actually getting to the bottom of the cause for a specific bug that is embedded deep in a programming language.
  • Using a debugger: The last common method of debugging is to utilize a separate program to debug your own program; these programs are called debuggers. This is typically the most powerful method as debuggers are designed to be multipurpose and to provide various approaches when it comes to keeping track of the changes taking place in your programs.

Most of the time, print debugging might be sufficient for your debugging purposes in small to medium programs. However, in a large system with many moving parts interacting with each other, using a debugger might prove useful to save time and improve your productivity. In the following subsection, we will be learning how to use the debugger provided by PyCharm and the various interactive functionalities it offers in the process of debugging.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.127.232