I'm looking for a better way to debug my code, ideally by having the computer suggest fixes f (rejected answer)
- 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 debugging techniques for Python:
-
Run your code through a syntax checker
-
Compare two versions of your code and find a small change that makes a big difference
-
Add print() statements or raise() statements to your code and verify that it behaves as you expect
-
Use the Python debugger, pdb, and the command help(pdb)
-
Use the Python tracer tool, the command help(trace)
-
View a call tree for your code, by running the command help(call_trace)
-
Use the Python profiler, the command help(profile)
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.