Debugging the bug free code
Tags: webdev, codenewbie, beginners, devjournal
Published: 2023-02-25 16:00:37

## Introduction

As a programmer, you might have come across the phrase "bug-free code". This term refers to software that is free of errors, glitches, and other defects that can cause it to malfunction. However, despite the rigorous testing, debugging, and quality assurance practices employed by programmers, it is virtually impossible to write completely bug-free code. In this blog post, we will discuss why bug-free code is a myth and provide my own example to illustrate this point.

## As a Software Dev

Over the years, I have developed software in various domains and it is important to understand that software development is a complex and dynamic process. It involves numerous stages, including planning, designing, coding, testing, debugging, and deployment. Each of these stages involves human intervention, and as we all know, humans are not perfect, sometimes even AI 😅. Even the most experienced and skilled programmers can make mistakes, overlook critical details, or misinterpret requirements.

Moreover, the software is often written to perform tasks in highly unpredictable environments, which can make it challenging to anticipate all possible scenarios. For example, a program designed to operate under a certain operating system may not work properly in another environment or with a different set of hardware. Similarly, a program that works perfectly fine in a specific situation may fail when unexpected inputs are encountered or when requirements are added over iteration.

## Background

Let's consider my example to illustrate this point. I want to describe a task that happened in the initial days of my career

### The Technology

The tech I was working on was Node js and was using MongoDB a popular NoSQL database that uses ObjectIDs as the primary key for its documents. While ObjectIDs are useful for uniquely identifying documents, they can also lead to issues if they are not properly interpreted in code.

### ObjectID Misinterpretation in Code

In MongoDB, an ObjectID is a 12-byte BSON type that consists of a timestamp, machine ID, process ID, and a random value. It is designed to be globally unique, making it an ideal primary key for documents in a database.

When querying for a document using its ObjectID in code, it is important to treat the ObjectID as an object, not a string. If the ObjectID is treated as a string, it may lead to unexpected behavior in the code.

For example, consider the following code snippet

```javascript
const objectId = '602f3d93a1e74a0112345673';
db.collection('mycollection').findOne({ _id: objectId }, (err, result) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log(result);
});
```

In this code, the ObjectID is treated as a string instead of an object. As a result, when the findOne method is called, it will not find the document with the given ObjectID.

Initially in my code, I didn't convert into an ObjectID because when the code is queried and passed to this condition it would be another object Id queried from MongoDB and it had no issues

## The Bug

The bug arose when there was a scenario where the object id was queried from a Redis data store which is an in-memory data structure where the ObjectID can be only stored as strings and it was directly passed to this query.

I was confused with what was happening as a few times the output came as expected when it was a direct query without involving Redis and a few times outputs were queried as null

### Code Logic Failure

When the ObjectID is misinterpreted as a string, it can lead to unexpected behavior in the code, such as not being able to find a document or finding the wrong document. This can cause the code logic to fail, leading to bugs and errors in the application.

For example, consider an e-commerce application that uses MongoDB to store orders. If the ObjectID is misinterpreted in the code, it may lead to the application finding the wrong order or failing to find the order altogether. This can result in the wrong item being shipped or the customer receiving the wrong order.

### Debugging Strategy

I put console statements on lines to debug but still was not able to figure it out. Then after spending some time on it, something struck me and I decided to check the type of data which is sent to the query. And then identified that we need to pass it as ObjectId

### Solution

The below solution worked where I would convert the string into an object id and then would pass in the query

```javascript
const objectId = new ObjectId('602f3d93a1e74a0112345673');
db.collection('mycollection').findOne({ _id: objectId }, (err, result) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log(result);
});
```

Misinterpreting Mongo ObjectIDs as strings instead of objects can lead to code logic failure, causing unexpected behavior in the code and leading to bugs and errors in the application. When working with ObjectIDs in code, it is important to treat them as objects and not as strings to ensure proper functionality and prevent errors.

## Lesson learned

While it is essential to strive for bug-free code, it is crucial to recognize that perfection is unattainable. Instead, programmers should focus on writing code that is as robust, reliable, and flexible as possible. They should also be prepared to identify, diagnose, and fix bugs as quickly and efficiently as possible. With the right tools, practices, and mindset, programmers can minimize the impact of bugs and deliver high-quality software that meets users' needs.

## Conclusion

I have faced much more bugs in my career and I would like to share a few insights which may help fellow developers

_Reproduce the bug:_ Try to reproduce the bug in your local environment. This will help you to understand the root cause of the issue and develop a fix.

_Debugging:_ Use debugging tools to step through the code and identify where the bug is occurring. This can help you to understand the flow of the code and identify the root cause of the issue.

_Communication:_ Communicate with other members of the team or the person who reported the bug. This can help you to get more context and background information that may help you to find a solution more quickly.

_Fix the root cause:_ Don't just fix the symptoms of the bug, but make sure to fix the root cause. This will help to prevent similar bugs from occurring in the future.

_Test the fix:_ Test the fix thoroughly to ensure that it resolves the bug and doesn't introduce new issues. Automated testing can be especially helpful for this.

_Document the solution:_ Document the solution and share it with the team. This can help others who may encounter similar issues in the future.

**Thank you so much if you have read it so far ✌🏻**

Connect with me via [Twitter](https://twitter.com/Dhanush_Nehru/), [Github](https://github.com/DhanushNehru), [Instagram](https://www.instagram.com/dhanush_nehru/)

_If you liked reading this blog, please don't forget to like ❤️, comment 👌🏻 and share 🤩 to show your support as your support means a lot to me._