Guide for fixing code defects

Spotting and fixing errors, bugs and faults in your code

Software development is more than writing code. A good part of the development process is discovering and dealing with defects in your code

While there may be technical differences among the terms error, bug, fault or defect, the aim of this article is to help you investigate and deal with any form of unexpected behavior in your code

The rest of this article will show you how you can find the cause of the defect and also give some insights on how to deal with them

Code changes

Did your code run and behave successfully before you made some update? If yes, then something you added or removed is likely the cause (or trigger) of the error

In some cases, what was introduced may not be wrong in itself, but it might present a conflict with some parts of the code already in existence.

In either case, actions to take include the following:

  1. Check the added code for local errors or conflicts with existing code

  2. Undo the changes (safely) and re-implement them in a step-wise manner, while running your code along the line

The need for frequent commits of your code comes to the fore here. In case the issue gets too messy to be figured out, you may be able to revert to a previous state and work through building up the code incrementally again

The error trigger

Did you get the error when you launched your app? Did it come up when you clicked on a button or went into a particular route? Logically examining the action that triggers the error will lead you into the part of the code that the issues are from. It will narrow the focus area of your investigation and will allow you get to the solution fast. Understand the trigger first – the solution may not be far

The error message

In case an error message is displayed, it is obvious that reading the message can point you to the cause of the fault. In some cases, the messages may not be so helpful in providing a direct cause of the error. Your experience and knowledge as a developer matter in such cases.

Depending on the language and IDE you are working on, the error message stack trace may show the root cause either at the top, bottom or somewhere else in the body of the trace. Be sure to understand the peculiarities of your language and IDE to make your effort successful

You are unlikely to be the first to have this problem. Copying the error message and pasting in a search engine can lead you to answers that have already been provided on the same issue. A good AI tool can also be of benefit in this case. Sites like stackoverflow.com, github.com, geeksforgeeks.org are among those that can give you ready answers. Take note to not just implement the recommended fix, but to understand the reason for the error and fix recommended

Run code in smaller bits

Of course, this will depend on the context of your code, as removing some lines or items can break down the entire code sometimes. However, you can start with what works till you get to what does not.

Running code in bits can also be done proactively – by ensuring code works correctly intermittently before the extent of changes becomes very large. This will reduce the possibility of coming up with complex faults and the incremental code to be examined will be smaller

Use of debugger

Most IDEs come with debuggers, which help to you step through your code and see intermediate results and execution flow. Typically, you will indicate a break point where you want the code to pause execution, for you to start to step through code and view intermediate results line by line. You should know what you are expecting to see, hence being able to assess if an unintended outcome has occurred when you compare it with the debugger results

Using the debugger can also help to know what part of the code was actually run and what part was skipped

Printing out

Adding console prints to critical parts of your code can give insight into flow of the execution. For example, if you want to know if the code execution got to a particular line, then you can add a print statement before the line and check to see if it is printed out on the console after the run. Also, print statement may be added to alternate paths for the code based on a conditional (e.g ‘if’ statement), to understand if an unexpected logical outcome has resulted

You should take note to remove unnecessary print statements after the code is completed

Conclusion

The foregoing has presented workable ways to trace defects in our code. While there may be other ways to go about this, these using one or more of these steps is likely to take you to the root cause of you code defect