DEV Community

Tiana Akinwale
Tiana Akinwale

Posted on

Mastering Code Errors

Working on a project as a developer can be so exhausting and mind-boggling a lot of the time, especially when an error is involved, and you cannot seem to figure it out. A wrongly placed semicolon could be the culprit, or it could even be a misread error from your text editor and it could keep you stuck for hours. I am sure you're smiling just remembering that one time. Often, we just cannot seem to figure it out and we end up rewriting the algorithm. If you're on that table or a newbie in tech, this article is for you.

In this article, I'll walk you step by step through my own experience and share tips on how to effectively debug and check for errors. I hope that after reading this, you'll find it easier to debug your code and help others do the same.

My Problematic Error
I was particularly excited about this project and I was eager to take on this new task. I encountered my first error, when I was trying to access a variable which I had not declared. I corrected my mistake and continued like a pro. Then, barely 20 mins later I encountered this similar error. This time the console wasn't much help and I was just staring at the screen, trying to figure out what went wrong.

Step 1: Don't Panic, Read the error message carefully
First things first, don’t panic. Errors are part of the journey. It’s easy to get frustrated, especially after sitting at your desk for long hours, but staying calm helps you think clearly. Then read the error message carefully. I know it sounds straightforward, but sometimes the error message is a riddle waiting to be solved. Read it carefully. In my case, the error message pointed me to an "undefined variable" on a certain line in my code. I checked the code line it referenced, but the variable was clearly defined. This added to my confusion.

Step 2: Trace back your recent changes
Think about what you changed recently. Errors often occur after you’ve made some changes. I remembered updating the code with a new function and a variable, so I started there. Even though the variable was defined, the error persisted. So I traced my changes by commenting out recently added code to the point where the code last worked perfectly. When I checked the command line, I found no error. I felt this was progress.

I started to re-introduce the commented code back gradually to trace where the error could be, and that was when I saw my mistake—I had declared two variables with similar names and used the other variable in my code. Fixing that resolved the error, and I could finally move forward with the project.

Steps 3 and 4: Use Debugging Tools and Get a Fresh Perspective
If you're still stuck, leverage on debugging tools to trace the error step-by-step. And sometimes, all it takes is a fresh pair of eyes. Talk to a friend or a colleague, but ensure you have exhausted the steps above before doing this. And you can use ChatGPT to help head you in the right direction to finding this error, but avoid relying on it for full code solutions.

Step 5: Test periodically
Once you think you’ve fixed the problem, test your solution thoroughly. I ran multiple test cases to make sure the error was really gone and didn’t introduce new bugs. And this is one way to avoid bugs, you test your code periodically.

Debugging can be a tedious and frustrating process, but it's also incredibly rewarding when you finally solve that pesky error. Remember, stay calm, read the error message carefully, use your debugging tools, simplify the problem, get a fresh perspective, and test thoroughly. I hope my experience and these steps help you in your debugging journey. Happy coding!

I recently started a backend development internship with HNG. I signed up because I loved to write code and solve solutions, and this was a great opportunity for me to do just that. I was full of joy when I got my acceptance email. And through this internship, I am expecting to build amazing solutions, collaborate with others and enhance my tech skills greatly with the HNG platform.
If you're passionate about coding and problem-solving like me, check out HNGor join HNG premium

Happy coding!

Top comments (0)