How to Debug Your Code

Written by Sophia on November 13, 2022.

Debugging is a big part of programming.

The term, bug, comes from an actual moth that crashed Grace Hopper's computer program. Since then, problems have been called bugs. Debugging means to solve a problem in your code.

These are some of my debugging strategies:

❓ Sanity check

  • Check that you're looking at your local environment, not production! I've made this mistake enough that it made it on this list.
  • Restart your program. Are you seeing any of the changes you made? Check that your code is running properly. Maybe hot reload isn't working, or it doesn't exist in the repository you're working in.

🤔 Understand the problem

  • What is your desired outcome? What is actually happening with your code changes? Clarify and break down your goals. Going through this process may help lead you to your aha moment.

🕵 Investigate

  • Delete (or comment out) code line by line to isolate the problem.
  • Step through your code line by line and console log different parts of your code. Is what prints out what you expect? I like to add a string, ******, to the logs, so I can easily find what I'm logging.
  • Use a debugger to add breakpoints and step through the code.
  • Google the error message. Chances are, someone has run into the same problem. Look for posts with similar examples or explanations.
  • Read the docs. Are you using an external library? Look at their examples and explanations.
  • Take a break! Limit yourself to 30 minutes of investigation. If you haven't found the bug, take a break. The answer may come to you while you're not focused on the bug. Revisit the problem with fresh eyes, you may find something new.

🗣 Talk it through

  • Explain your problem to a rubber duck, or anything else that doesn't talk. The process of explaining something uses a different process than debugging by yourself. Talking through the problem step-by-step may help reveal details you initially missed.
  • Ask for help or pair with someone. Be sure to share the context of the bug and what you've already tried.
  • Once you've solved the problem, share or jot down your learnings. You might run into this again or make someone's day!

Up Next