How do you deal with bugs?
Well, first by trying to avoid them as much as possible, by carefully thinking about how your program should work, even before you write a single line of code.
Then, by analyzing every single line of code you wrote for possible issues or side effects or unconsidered things.
And.. once you know there’s a bug, how do you solve it? Well, the hardest part is always identifying the bug. Then the second hardest part is figuring out why this bug happens. Solving the bug is generally easy once you know all of the above.
Generally we can do two things to solve the bug.
One technique is very basic and involves trying to figure out the values of the state (the content of the variables), and the flow of the program, and printing the to the logs, or to the output of your program. It’s easier with CLI apps.
Using a debugger you will be able to stop the running of the program at any time you want, watch the content of the variables, execute any code you want, and step through the program execution one line of code at a time. It’s very important to debug programs that don’t work as you expect.
I won’t go into more details. Just know that there are tools that can help you fight bugs.
Testing is another one of such tools.