Tips for Learning About and Debugging Your Code

pythondebugging

1. Print everything. No, really, everything.

Print something at the beginning or end of a method or function to know if it’s being called. Print what your variables are. Print what your variables’ classes or types are. There is no such thing as too much information about what your code is doing.

>>> a = [1,2,3,4]
>>> print(a)
[1, 2, 3, 4]
>>> print(a.__class__)
<class 'list'>

Don’t forget to label what you’re print-ing to make sure you know which variables are which.

This:

>>> print("a.__class__:", a.__class__)
a.__class__: <class 'list'>

Is clearer than this:

>>> print(a.__class__)
<class 'list'>

This labeling is indispensable when you’re printing multiple variables in a file or project. It’s very easy to forget which variable in your output is which, especially if many of them are similar.

In Web Apps

If you’re writing a web app, printing will generally be visible in your server logs. You can also show variables’ values directly on your web page.

More On Printing

Check out this blog post about puts debugging in Ruby.

2. Always, always, always Google things

Google your problems, google your errors, google things you’re not sure how to use, google for whether or not there are easier ways to do what you’re doing.

Things to put in your Google search

  • The language or library you’re using (e.g. Python, Javascript, React, Django)
  • A direct copy-and-paste of an error you’re getting
  • “How to [the thing you’re doing]”
  • “code example of [the thing you’re doing]”
  • “documentation for [the class of the object you’re trying to use (e.g. list, file, dict)]”
    • If you don’t know the class, print it (see above)

Search results that are useful to look at

  • Documentation for the classes or libraries you’re using (e.g. list, dict, django, request, etc.)
  • Stack Overflow answers to your question
  • Code examples
  • Stack Overflow questions that don’t answer your question but are dealing with the same or similar operations (they may not answer your question directly, but they will have code that may be useful reference for roughly how to do it)
  • If you’re trying to figure out how to get information out of a particular object, figure out the class of the object you’re using and google the class name to find examples or documentation

Someone has probably solved your problem before

Always try to take a second to step back and ask yourself: do you think what you’re trying to do seems like something a lot of other programmers would probably need to do sometimes? If so, there are almost certainly written solutions for it, and there’s a good chance, if it’s a simple operation, that there’s already a simple built-in function or method for it. One of my favorite discoveries of built-in functionality ever was finding out that Ruby on Rails provides the distance_of_time_in_words_to_now method out of the box. If Ruby and Rails can ship with solutions to problems that specific, there’s a good shot your tools may have a solution to any simple problems you’ve come across, too.

Particularly if you’re a relatively new programmer, it will be pretty rare for you to come across a problem that hasn’t already been encountered by literally thousands of programmers before you. Don’t re-invent the wheel if you don’t have to.

3. Use whatever debugging tools are available to you

  • Text editor features and plugins: Some text editors will have features that will point out possible errors, unused variables, etc, for you.
  • Pretty printing: Many languages have “pretty printing” libraries that give you ways to print your variables and data with some nice colorization and formatting that make them easier to read.
  • Static analysis tools: many languages have “static analysis” libraries that can scan your code for various types of issues — security flaws, database query issues, etc.
  • Debuggers: many tools, text editors, and most browsers also have built-in debuggers that let you set “break points” to pause your code and look at what is happening, what variables exist, and what values those variables at any point in your code’s execution.