Improving software debugging skills - some tips
From the translator. Debugging software for many coders is a boring and routine exercise. But still, it's impossible to do without debugging. This post is a translation of the original article by Nick Karnik, in which he shares his own observations about errors and debugging issues that are found not only among novice programmers, but also among pros.
We all write code, which sometimes, unfortunately, refuses to work. This is normal, just part of the development process. If the program gives an error, sometimes you just do not know what to do next and how to fix it. Do not worry - all problems are solved, and errors occur even for the most experienced coders.
Skillbox recommends: Practical annual course "PHP developer from scratch to PRO" .
We remind: for all readers of "Habr" - a discount of 10,000 rubles when writing to any Skillbox course on the promotional code "Habr".
In order to make fewer errors, it is necessary to develop skills like programming and debugging.
They themselves are divided into two types - logical and syntactic.
The most common mistakes of developers
Missing Error Messages
One of the most complex scenarios is when the application crashes and there are no reports of the cause of the incident. In order to be able to correct a problem in the code, you need to understand whether it occurs during startup or already in progress. You can find out by adding logging of application messages at the beginning of the code.
If there is no log, it is most likely that the program crashes during the download, even before something starts to be written to the log.
If you have a message log, go through it and find a possible cause of crash. In order to narrow the circle of the search, it is necessary to make the program write to the log the main events in the course of its work. In this case, the problem will be easier to find.
"Phobia" error messages
If we are talking about the frontend, then errors are usually displayed either in the application itself, in the user interface, or in the developer console. Sometimes messages can be seen in the backend using the terminal or in the event log. Regardless of the type of errors, newbies get scared of such messages and don’t even read them.
This is the main reason why debugging takes so long for some developers.
First of all it is necessary to study the error, having disassembled everything very carefully.
Reluctance to learn system logs
Some programs write log files or generate an event log. All this is just a storehouse of useful information. If it was not possible to find an exact indication of the cause of the error in the event log, try searching for failure messages or, on the contrary, successful launch of something (module, service). This will help to understand the cause of the error.
No trace log
Tracing helps to track the process of the program and work with data. Writing trace messages while the software is running simplifies the debugging process. The trace log is a simple way to learn how your application works.
Inability to work in stages
Many developers write large chunks of code, then compile and test them as part of the application. But it is worth remembering that the search time for bugs in the code is proportional to its volume.
If there are a lot of changes, you will have to spend a lot of time searching for a problem that suddenly appears. It is best to work in stages. Make small changes, compile the code, test the application - and then repeat it all. When you implement changes in small iterations, it ultimately speeds up the process of creating an application. If something does not work, you can quickly understand why.
After writing my code, I often conduct its revision to simplify writing.
Reluctance to perform automatic testing
Unit-tests and end-to-end test automation allow you to identify potential errors as they occur. One of the reasons for the errors is that developers refactor the code before it is thoroughly checked, that is, the changes are not automatically tested.
Inability to work with the exclusion method
If you cannot determine the root cause of your problem, you need to use the exclusion method. You can comment out new code blocks to see if the error disappears. This helps to diagnose the problem.
In the process of debugging, you can form a certain hypothesis and try to prove or disprove it using the method proposed above.
Copy Paste with StackOverflow
Often, developers copy-paste the code from StackOverflow, without having a clear idea of what exactly the copied site does. Such actions have a number of negative consequences, some of which are implicit. Be sure to pay attention to what you are adding, try to understand what functions the section of the inserted code is responsible for.
By the way, quite often, when I ask a question on StackOverflow, a little later I answer it myself, because I’m thinking about a problem in the process of writing.
The same happens during the discussion of problems in the composition of their team. You yourself find a solution to your question, simply because you are thinking about the cause of the problem.
Reluctance to re-inspect code
One of the most successful debugging methods that I know is to study your code - again and again. In some cases, writing a piece of code from scratch helps.
Inability to trace function calls
Sometimes it also helps to study the function calls in your application. You just need to check the variables and their expected values. In the process, you can find problem areas in your program, which lead to its strange behavior.
Ignorance of the debugger
You definitely need to learn how to use the debugger. This is the best investment of time. All IDEs come with powerful debugging tools that work about the same. Learn these tools — and you will solve most of your problems when writing code.