Debugging

Misc

  • Rubber Duck Method
    1. Beg, borrow, steal, buy, fabricate or otherwise obtain a rubber duck (bathtub variety).
      • Note: In a pinch a coworker might be able to substitute for the duck, however, it is often preferred to confide mistakes to the duck instead of your coworker.
    2. Place rubber duck on desk and inform it you are just going to go over some code with it, if that’s all right.
    3. Explain to the duck what your code is supposed to do, and then go into detail and explain your code line by line.
    4. At some point you will tell the duck what you are doing next and then realise that that is not in fact what you are actually doing. The duck will sit there serenely, happy in the knowledge that it has helped you on your way.
  • Silver Searcher - Fast code searching tool
    • Basic usage for debugging: ag "my error message"
    • Example (source)
      • heroku run executes a script and the error message has the words “could not be mailed.”
      • From your local project directory, ag takes that message string as input and outputs the paths to the files with that string and the line of code with line number.

R

  • Functions (From userR 2024, need to get link or person)

Python

  • breakpoint (Docs)
    • Place function on the line above the line that you think is causing the error
    • Once you’re in the python debugger (PDB), you can run any python code you want, just as if you were in a REPL. So once you finde the exact place that has caused the error, you try different solutions (e.g. setting variables to different values, importing functions, etc.) in order to see if it works.
    • Example (source)
      • Add breakpoint to script
        • Error from the except line was tripped, so the problem must be in the try chunk
        • The problem isn’t print or sleep, so breakpoint is placed above the send_message_as_pybites function
      • Rerun script that gave the error
        • The line below the breakpoint function is returned and PDP (Python DeBugger) is opened
        • l or list command returns 11 lines around the returned line which is send_message_as_pybites (the arrow marks where the returned line is)
      • Step into the function
        • Since the line below the breakpoint is a function, you can used s or step to “step” into that function and look at its code.
        • The output shows us it is now in the utils.py file
        • Now, using the command l again shows 11 lines around the function definition line
      • Use next to execute the next line of code
        • n or next executes the next line of code. It can also be used on function calls. While step goes to the function, next just executes the line.
          • If you type an of the variables (e.g. sender, receiver, etc.), it’ll show you the current value
        • Using l again shows 11 lines around the current line and allows you to see where you are in the scipt.
      • Continue with next until the error is triggered
        • next executes the Thread function to create a thread object which is fine, but when thread.save is executed in the next step, the error is triggered.