Debugging Techniques

This document lists a few different simple debugging techniques.

Debugging as a Murder Mystery

Are you an expert at solving murder mysteries? Thankfully, you can put that expertise to use in programming as well! Here's how to solve a problem when Python is giving you an error message but you're not sure why:

  1. What is the murder weapon? Read what the error message says, and try to figure out some hints about which operation actually caused the error, and which variables/values were involved. Try to identify the specific operation (e.g., function call, operator, variable assignment, etc.) that caused the problem.
  2. Who are the suspects? Identify every variable and value on the line where the error occurs. For each suspect (i.e., variable or value) follow these steps:
    1. Check if the suspect is really a person, and not an elaborate ruse. Did you misspell a variable name? Have you used the correct syntax for defining a value? Did you use leave off quotation marks (indicating a variable) when you meant to use a string?
    2. Check if the suspect is wearing a disguise or using an assumed name. First, come up with an expectation for the type of each value, and for both the type and the value of each variable. Then, add a print function call to your code to print out what the actual types/values are (add this print immediately before the line where the error occurs). If there's a value or variable whose type or value doesn't match your expectations, you may need to investigate further.
    3. For suspects of interest check how they entered the crime scene. For each mismatched variable from the previous step, identify where its value was most recently updated, and figure out how to get it a more appropriate value. You may also need to change its name to match its value if the value is something that you do in fact need to use.
  3. If at this point, the perpetrator is still unclear, you may need to fully reconstruct the crime scene step-by-step. Take apart the expression where the error occurs and turn it into a series of single-operation statements using intermediate variables. With only one thing happening at once, the error should be easier to pinpoint.