Pragmatic Programming

Learning how to debug issues, without losing your temper, is one of the most important parts of being a developer.

It's easy to get frustrated by code. In your head, you know what you want to do. But sometimes, the code is just not working how you want it to - and there can be a million reasons why.

In these situations, it's important to take a pragmatic approach and look at the issue logically. Remember that the code is doing exactly what you are telling it to do. That's the only way computers work. So if the code is not doing what you want it to do, maybe your mental model is wrong.

This often happens if you're coding in a language or framework that you're not familiar with, but it's also possible that you've just been blindsided. When we work on the same problem for a long period of time, our brain starts to assume things that might not be true. There's a few good ways to deal with this:

  • Read through your code line by line . You might be overlooking a simple thing that you're not thinking about. Go into each function and make sure it looks the way you intend.
  • Ask for a second pair of eyes. Sometimes you miss obvious things when you're too close to the problem. Asking a co-worker or someone online for help can often point out the mistake.
  • Talk through your solution out loud (rubber duck debugging). This is one of the oldest tricks in the book. It involves forcing yourself to explain your code to an inanimate object (like a rubber duck - but you can use any random object here). The act of actually speakingand articulating your solution often leads to an "aha" moment, where you realize the fault in your logic.
  • Take a break. Sometimes the most effective debugging tool is a breath of fresh air, a quick bite to eat, or (my personal favorite) a good night's sleep. Giving your brain time to rest, and coming back with a refreshed mind, will allow you to approach the problem logically. You'll see important details that you missed the day before. And problems that might have kept you stuck for hours, are suddenly solved in 5 minutes.

Whatever you do, always try to keep your head clear and focus objectively on the task at hand. That'll make it much easier to deal with than if you start getting frustrated!