Create Debugging Strategies authored by Gregory Neibert's avatar Gregory Neibert
## Incremental Development
The easiest way to debug is by forcing bugs into the smallest area possible. This can be achieved by incremental development.
By using TDD (which you already are, right?), incremental development is already being put into action. By testing small bits of code as they are created, the area in which a new bug could have occurred is greatly limited.
Combining this knowledge with the following strategies should, in theory, make debugging a breeze, as only this small area needs to be searched.
## Error Output Analysis
When an issue arises that causes a test to fail or the system to crash, an error message is always printed along with it. These error messages are often times extremely useful (at least in Java; not everything is just a segfault in this language) and should be read carefully.
Generally speaking, the error message will give a rough location of where the error occurred, at worst, and the exact location, at best. For most simple issues, it will state exactly what went wrong, as shown below.
<p align="center">
<img src="https://i.imgur.com/dN3H9z2.png" />
</p>
For more advanced problems, or when a test fails, you will probably receive a wall of red text. Do not panic. In both cases, a stack trace is printed, which makes up the majority of the wall. Generally speaking, the most recent blue link in where the error occurred. By working backwards from this link, and using other debugging strategies, the root of the issue can be found. For example:
<p align="center">
<img src="https://i.imgur.com/2jWevSs.png" />
</p>
<p align="center">
<img src="https://i.imgur.com/xJq8x8V.png" />
</p>
<p align="center">
<img src="https://i.imgur.com/YiefEsT.png" />
</p>
This is a rather simple example, however, the basics remain the same for all issues.
## Confirming your inputs
If a source to your problem cannot be found, confirm that your inputs will, in fact, produce the expected result, assuming the code is working as intended. Sometimes the "issue" cannot be found because there was no issue with the actual code in the first place. Rather, it is the test that is incorrect.
## Use the Debugger
Often times it is easier to solve an issue by using the Debugger.
[How to Debug in Intellij](Debugging-In-Intellij)
## Take a break
If you have been looking at the code for a long time without making any headway, it might be a good idea to walk away for a bit. Frustration often blinds us, so coming back with a clear mind can make it much easier to solve the issue, and will save you a lot of time in the end.
\ No newline at end of file