CodeGym /Courses /Python SELF EN /Debugging a Program

Debugging a Program

Python SELF EN
Level 18 , Lesson 2
Available

8.1 Emergence of Bugs

Debugging software is the process of finding and fixing errors or bugs in a program. This process is one of the most interesting and important parts of software development, helping ensure programs run correctly.

The History of the Term "Bug"

The term "bug" in the context of computer programs was first used in the 1940s. Although the concept of "bugs" existed even before computers, referring to mechanical malfunctions in various devices.

The first documented use of the term "bug" in computing was with a team working on the Mark II computer at Harvard University in 1947(!). The team found that one of the machine's relays wasn't working because a moth had gotten in. They removed the insect and recorded it in their log as "the first real case of bug being found."

Even though this wasn't the first instance of using "bug" to indicate an error, this incident became famous and helped popularize the term "bug."

Debugging a Program

Debugging is the process of finding, diagnosing, and fixing bugs in software. The debugging process includes several steps:

  • Error Detection: Identifying symptoms that indicate the presence of an error in the program.
  • Error Reproduction: Creating conditions under which the error appears again to better understand its nature.
  • Problem Diagnosis: Using various tools and methods to analyze the code and find the cause of the error.
  • Error Correction: Making changes to the code to fix the error.
  • Testing: Checking the fix and testing the program to ensure that the error is fixed and no new problems appear.

So, a bug is a general term for any errors in programs, and debugging is the process of finding and fixing these errors.

8.2 Tools and Methods for Debugging

Logging

Adding logs to a program’s code lets you track its execution and get information about the state of variables and operations at different points in the program's execution. Python has a great library for logging — logging.

Example of using logging:


import logging

# Setting up logging to output debugging information
logging.basicConfig(level=logging.DEBUG)
            

def divide(a, b):
    # Logging the attempt to divide
    logging.debug(f"Dividing {a} by {b}")
    if b == 0:
        # Logging an error if b is 0
        logging.error("Attempt to divide by zero!")
        return None

    # Perform division if b is not 0
    return a / b
            
result = divide(10, 2)
print(result)

Using Debuggers(Debuggers)

Debuggers are tools that let developers execute programs step-by-step, set breakpoints, check variable values, and change them during program execution.

Popular debuggers:

  1. GDB: GNU Debugger for C, C++, and other programming languages.
  2. PDB: Built-in Python debugger.
  3. Visual Studio Debugger: Debugger built into Microsoft Visual Studio.
  4. PyCharm Debugger: Debugger built into the PyCharm IDE for Python.

Example of using PDB


import pdb

def faulty_function(a, b):
    pdb.set_trace()  # Setting a breakpoint
    result = a / b
    return result
            
faulty_function(10, 0)

When the program hits pdb.set_trace(), it stops, and you can execute debugging commands like next (next step), print (output variable value), and others.

We will be learning how to debug programs using the debugger built into PyCharm.

8.3 Using Debug

In general, debug involves actively using debugging tools and practices to identify and fix errors.

Setting Breakpoints: Set breakpoints in your code to stop program execution at points of interest.

Step-by-Step Execution: Execute the program step by step, observing changes in the program state and variable values.

Variable Analysis: Study variable values and expressions at various stages of program execution to identify incorrect data or logical errors.

Fixing Errors: Make changes to the code to fix the identified errors.

Retesting: After fixing errors, test the program to ensure the error is fixed and no new problems have arisen.

Seems simple, right? In the next lecture, we'll go through all the steps in detail :)

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION