Debugging Intro


What is debugging?

Debugging is the process in which programmers fix their code to make it work as intended.
To debug, programmers often print out variable contents before and after some calculations.


Here is an efficient method I use to debug.

Make and Utilize Comments

					// Input        (take user input)

					// Process      (use the input to perform actions)
					//  Action 1
					//  Action 2

					// Output       (print the result)

I use the outline above for most of my programs in competitive programming.
What makes this outline good are the comments that organize your code neatly.

When you are double-checking your code, the comments will tell you exactly what is intended to occur.
When other programmers look at your code, they will understand the code faster and maybe help you fix the problem quicker!


Utilizing Comments

Sometimes, you might struggle with finding the error in your code.
In such situations, printing out your variables or arrays before and after some calculations will help you locate where the error is coming from.

					for (int i = 0; i < n; i++) {
						for (int j = n - i - 1; j >= 0; j++) {
							// ... code here
						}
					}
				

If there is a Runtime Error or a Time-Limit-Exceeded Error, commenting out a line or multiple lines can help you determine the cause of the error.
In the code above, the j++ makes the for-loop run forever, resulting in a Time-Limit-Exceeded Error.
Once that for-loop block is commented, the code runs smoothly, tellling us that the error is somewhere in this for-loop.


That is it for the basics of debugging!
Hope you learned something new from this article!