Coding Principles

|

5

minutes read

The key to successful debugging - Always find the root cause

Travis

Ponyo

September 19, 2022

feature image

Real developers code with KISS

Real developers code with KISS

Travis

Ponyo

September 19, 2022

The Principle of Least Surprise

The Principle of Least Surprise

Travis

Ponyo

September 19, 2022

Bugs are an inevitable part of coding. Even developers with years of experience have probably never written a bug-free application.  A bug can result from many reasons, for example, incorrectly written code, errors in a compiler, or even hardware issues.

Journey to the end of the “bug”

There are many ways to debug. You can try either

  • Rerunning the failed process and monitoring the change of data
  • Googling for solutions, the most common place for all the solutions to your bugs is the infamous StackOverflow
  • Asking an expert for help - when all is failed, turn to your senior

However, the debugging process does not simply end with finding the solution. The ultimate destination is to identify the root cause.

🐜 One common mistake among developers is that they usually let the “bug” slide once they’ve found the solution, rather than digging to find the root cause.

Where to find the root cause, you may ask. Well, the answer lies in the Five Whys.

Why, Why, Why, Why, and Why

Five Whys analysis is a basic yet practical method for determining the root cause of an issue. It’s also important to bear in mind that the 5 Whys isn’t about apportioning blame; it is about understanding the root cause of a problem and then putting in place countermeasures to prevent it from happening again.

Here are the steps on how you can carry it out:

  1. Begin with a specific problem. What is it that you are having an issue with? This can also help the team focus on the same problem.
  2. Ask why the problem happened and write the answer down below the specific problem you listed in step one.
  3. Keep asking “why” to each of the successive answers you write down until you reach the root cause of the problem.

Sometimes it may take more or less than five why’s to reach the root cause of your problem. However, you should always make sure that your team sees eye-to-eye with each of the questions being answered as well as the final root.

a meme about using isset() to fix an undefined index notice in programming

When you choose to do the quick'n'dirty instead of fixing the root cause for that missing array index.

Key things you should note from the Five Why’s

  1. Distinguish causes from symptoms or causal factors
  2. Try working backward to make sure that you are attributing the correct answer to each “why”. (Answer to the “Why?” + “and therefore” + the Problem Identified for that Question)
  3. Break down your answers as much as you like, the more the better
  4. Answers should always be based on facts and data
  5. Last but not least, assess the process, not the people
🐜 Identifying and solving the root cause is how you can ensure the code quality, and also prevent future codes from catching the same bugs.
a meme about a developer finally found the bug in his project - his stupidity

Closing

Bugs are part of the software development process. So if there is a bug in the code you wrote, you're in good company. However, always remember to find the root cause of the bug, instead of going for a quick fix, so that you can prevent those bugs from reoccurring in a new form in the future.

Happy coding, folks, and don’t forget to catch our upcoming coding techniques from FABA Technology.