Zen of debugging: 5 tips for beginners

huntereducative

Hunter Johnson

Posted on May 2, 2024

Zen of debugging: 5 tips for beginners

I'm Fahim, a software developer turned tech founder. This article is part of my series: The Developer’s Launchpad. I'll share my top lessons, hacks, and best practices for learning how to code and launching a new career — things I wish I would've known earlier in my journey. If you're starting out your own coding journey, this series is for you.

Bugs — they happen to the best of us.

A bug is any issue in your code that is causing unexpected or undesired results. Bugs are an inevitable part of coding. As a result, learning how to resolve bugs (i.e. debugging) is an essential skill for your long-term success.

In my experience, the best programmers are also the best at debugging. Learning to debug effectively will help you better understand coding concepts, get more familiar with your code, and — in the long run — will make you more efficient at work.

If you're a complete beginner or an early-stage coder, I'll be sharing some perspectives to help you hit the ground running and become more effective at debugging.

Debugging and development

Let's do a quick rundown on bugs first.

Getting comfortable with bugs

In programming lore, the first computer "bug" was supposedly caused by a moth stuck in a computer at Harvard University.

If you don't like insects, you can at least find relief in the fact that most coding bugs don't involve actual bugs. That said, debugging can be far more complicated than removing a misplaced insect.

Software development is a complex process that works with various interconnected lines of code. Even if you know a given codebase, you may still inadvertently make a change that negatively affects the entire system (just as trying a new ingredient in a trusted recipe can unexpectedly yield negative results). You may have a bug arise simply because it's being run on a different hardware setup for which it hasn't been made compatible. Better yet, even if you didn't do anything yourself, your software can suffer from bugs that originate from their _external_dependencies, such as third-party libraries.

In any case, bugs are inevitable. Whether you like it or not, you'll have to get comfortable with them.

Source:

Ideally, you should hopefully find that you even enjoy debugging. There is certainly a great reward that comes with finally finding the source of the issues plaguing your code. It can be a challenge, but if you're curious, you'll only come out the other side having a stronger understanding of your code (and stronger problem-solving skills).

Oh, the bugs you'll find!

There are various types of software bugs.

Many bugs fall into two categories: syntax errors and semantic errors.

Syntax errors have to do with mistakes in your code's syntax, including spelling, punctuation, improper naming, or omitting definitions for variables. On the other hand, semantic errors have to do with meaning. Semantic errors don't stop the program from running — rather, the program does not operate as intended.

That said, bugs can also be grouped into other categories (some of which overlap with syntax and semantic errors):

  • Runtime errors: Invalid operations or insufficient memory can cause crashes or unexpected behaviors during the execution of a program.
  • Compilation errors: With compiled languages, these errors interfere with code being converted into executable form. They can be caused by syntax errors, types mismatches, etc.
  • Logic errors: These errors are correct in syntax but don't perform the intended operation, leading to incorrect output or behavior.
  • Concurrency bugs: These occur when the timing and sequence of executing concurrent or parallel programming techniques lead to unpredictable or incorrect outcomes.
  • Resource leaks: These happen when a program fails to release resources, like memory, file handles, or network connections.
  • Security vulnerabilities: These involve weaknesses that can be exploited to cause damage or gain unauthorized access.
  • Interface bugs: These stem from problems in the interaction between parts within the software, or between the software and external systems or hardware.

You won't encounter many of these bugs until you deal with more complex software. However, it helps to acquaint yourself with the different types, so you can discern the type that you're dealing with.

You're only human

Sometimes bugs will arise as a result of your human flaws, and that is okay!

I've experienced this hundreds of times. At first I think the code is right, and I'm convinced the problem is the compiler or the language... then I eventually realize the problem was me.

We're human and we make human errors, whether they're syntax errors or forgetting some nuances about our given programming language.

Keep calm, and try to have a sense of humor about it. It’s all part of the process!

Image description

If you're stuck on a bug and don't know what's gone wrong, it can be frustrating.

But it will happen… so here are some helpful strategies you can try:

1. Understand the problem

Ask yourself these questions to understand your problem:

  • What should the code be doing?
  • What is the code actually doing? What are the conditions under which the unwanted behavior occurs?
  • Are there any error messages that indicate specific problems?
  • Do you know where the bug is originating? Have you broken the code down to isolate the section that contains the bug?

Be resourceful as you try to understand what's gone wrong. Make sure that you've been correctly using any functions, methods, or features by referencing documentation and other resources.

2. Talk to yourself aloud

Calmly walk yourself through the problem at hand and how you've approached it. As you explain aloud, you may discover assumptions you've made or details you've missed along the way. This strategy is effective for many developers, and is what we call "rubber duck debugging." (Rubber ducks aren't necessary, but they do add a playful touch.)

3. Use debugging tools

There are various tools that will help you debug, and you should use them when you can! You can use specialized debugging software or built-in language tools like Python’s pdb module.
Many integrated development environments (IDEs) include debugging tools as well.

4. Seek feedback

The coding community is very willing to share knowledge and give feedback, so don't hesitate to ask for help from other developers. It can be as simple as posting your problem in an online forum (with the bonus of knowing your query could help other coders in a similar situation).

5. (Last but not least) … Sleep on it

Sometimes, all it takes to solve what seems to be an insurmountable problem is to look at it with fresh eyes. If you’re stuck on a problem and don't know how to move forward, sometimes the best thing to do is walk away. Just disconnect from the problem entirely. If it comes down to it, sleep on it.

Patience, persistence, & curiosity

Image description

Remember, debugging isn't about speed. It requires patience, persistence, curiosity, and attention to detail.

Debugging is one of many problem-solving processes that are the foundation of software development.

The better you get at debugging, the better a coder you'll be. Over time, I hope you even learn to enjoy the debugging process.

As a reminder, you can find everything you need to go from your first line of code to your first job with Educative's Learn to Code resources — courses, Skill Paths, and projects specifically for beginners.

More from The Developer's Launchpad:

💖 💪 🙅 🚩
huntereducative
Hunter Johnson

Posted on May 2, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related