When you're working closely with students every day, you get really good at debugging. I pretty much see bugs all day everyday. Some are standard issues that I came across when I was learning the material myself, while others are really strange edge cases I may never have discovered on my own.
Helping students squash bugs is great, but what's even better is teaching them the skills they need to squash bugs on their own. In this post, I'd like to highlight a few of the techniques you can use to more effectively debug your code, and how to ask the right questions to help you track down the source of your problem.
Debugging is a skill, and like any other skill, it takes dedicated practice to master. It's also really important if you want to become a successful developer. As I wrote in an earlier blog post, it's really important to strive to become self-reliant in your debugging. When you need to ask for help, there's nothing worse than saying something like "This is broken, can you fix it?" The more information you can provide about what's broken, the better. And more often than not, in the process of gathering that information, you'll be able to fix the issue yourself.
Here are three things that you should always be doing when your application isn't working.
Read the Stack Trace
The stack trace is the long message that you see when an error gets thrown, that provides details about where in the code the error is occurring. It's an intimidating thing for novice programmings, but I've found that sometimes even people who have been coding for a while aren't very comfortable with the stack trace, and will quickly throw up their hands and give up when they see one.
Here's an example of the stack trace for an error on this website. The trace itself is on the left. It's definitely intimidating the first time you see it, but professional developers deal with screens like this all the time. The most important thing to keep in mind is that the trace will almost always include lines of code you haven't touched, and may not know anything about. 99.9% of the time, you can ignore those lines. The issue is almost always with some code you've written, not with some other library you're using. So find lines in the trace that point to code you've already written, and begin your investigation there!
Pause Code Execution
When people are first learning JavaScript, they love to put console.log
statements in their code to inspect the values of certain variables while their programs are running. This isn't necessarily a bad way to debug your code, but it doesn't scale very well: if you put tons of console.log
statements in your code, your log can quickly become confusing, and it can be hard to remember where exactly the logged expressions are coming from.
You'll debug your code much faster if you can pause the execution of the code and actively inspect the values of different variables at that point in your program. In JavaScript you can do this with the debugger
keyword, and other languages offer similar tools. Chrome also offers handy tools for debugging JavaScript directly in the browser. We've got a screencast on using those tools, which you can see here.
Search the Internet
When all else fails, use the hive mind! It's unlikely that you're the first person to have ever encountered the error that's blocking you. So go to Google, go to Stack Overflow, and see what other people have to say about related issues.
As a beginner, however, this is easier said than done. Sometimes you may not have the vocabulary you need in order to Google for the solution that's out there. Or you may think you know, but then spend half an hour in a fruitless search for an answer.
But that's okay! It's much better to ask "Hey, I'm stuck on this bug, here are the things I've tried, and I'm not quite sure what to Google for. Can you give me some suggestions?" than it is to ask "Hey, I'm stuck on this bug, here are the things I've tried, can you fix it?" When you're trying to build your own self reliance, look for the question that will help you solve the current issue AND give you a leg up on any future issues.
These skills may seem obvious to seasoned developers, but I've found that they're some of the most critical for beginning programmers, and also some of the easiest to overlook. We're all mesmerized by the latest tools and technologies, but investing in fundamental debugging skills will pay off no matter what technologies you use.
So if you're new to programming, try to get comfortable with error messages and debugging. If you're seasoned, try to help out someone who's new. And if you're really stuck on a problem, maybe you can find an answer in one of our online courses!