I'm looking for a better way to debug my code, ideally by having the computer suggest fixes f
- User
Human: I'm looking for a better way to debug my code, ideally by having the computer suggest fixes for me. Can you suggest some techniques that might be useful?
- Assistant
Here are some suggestions:
-
Use automated tests to check your code
-
Step through your code with a debugger
-
Be specific with your error messages
-
Put a guard in your code that’s triggered when the value of a variable or expression is undefined
-
Put a guard in your code that’s triggered when a key isn’t in a dictionary, or when a sequence of items doesn’t have a certain number of elements
-
When writing your code, be aware of the principles of syntax, of the principle of least astonishment, of YAGNI, of the DRY principle, and of the “we have one brain” principle
-
When you discover that you’re solving the wrong problem, consider rethinking the overall design of your system
-
Avoid bugs related to infinite loops and race conditions
-
When working on a complex system, strive for architectural modularity
-
Avoid bugs related to unchecked exceptions and parameter errors
-
- User
Those are useful techniques to try and avoid having errors in the first place, but I already wrote some code with a bug in it. What's a good debugging tool or approach for Python?
- Assistant
Here are some general approaches:
-
Use the Python debugger called pdb
-
Put logging statements in your code, to help track down where in the code errors occur
-
Use asserts to make the computer verify the code was written correctly
-
Put pdb statements in your code, which allow you to step through your code and inspect values
-
Use the python debugger pdb to track down where in the code errors occur
-
Add informative print statements in your code to help you find where errors occur
-
Use assert statements to make sure your code behaves correctly
-
Write helper functions to use in your code, to make it easier to determine where an error occurred
-
Make errors easier to catch by explicitly handling possible exceptions
-
Change the default python output to be verbose (logging statements, line numbers)
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.