Everyone wants to have their program run perfectly without any errors. However, making sure that a program is working as expected is not simple. When a runtime error occurs or the program runs, but it does something different, we need to find out the cause. This can be very time consuming and can lead to a massive headache! Especially when the program is written out of many lines of code.
Debugging is an essential skill to have, and it takes time to be good at it. It is needed to develop programs without any error. Beginner programmers usually stare at the code they have written and hope that they can find the cause, which is not the best way to fix a program. There is an easier and more efficient way of debugging a program. Most IDE provides a debug mode, which is running the program step by step to allow the programmer to see how their code works. This way, the programmer can easily spot errors in their program. This post will be discussing how to debug your application using debug mode in Visual Studio.
Debug mode in Visual Studio
Before you start running your program in debug mode, you need to set a breakpoint in your source code. To insert a breakpoint, select the line where you want to put the breakpoint, then press F9 (If you are not sure where to put it, add a breakpoint at the line before a part of code you want to examine). A small red dot will appear in the left margin, before the line number. Note that it is possible to insert more than one breakpoint.
Next, to start your program in debug mode, press F5. Your program will pause once it reaches the line where you put the breakpoint. When it breaks, you can look at the values of variables, process memory, CPU usage, and more.
There are buttons to continue executing your program step by step. They are: step over, step into, and step out. All three buttons appear only when you are running your program in debug mode, located at the top.
1. Step over
Step over moves the debugger to the next statement in the code. When the current statement is calling a method, using step over will skip the code inside that method (each line still runs, but they are not debugged) and move on to the next statement after calling the method.
Alternatively, we can also use the keyboard shortcut instead of pressing the button. The keyboard shortcut to step over is F10.
2. Step into
This feature works similarly with step over. However, using step into when the current statement is calling a method will bring you into the code inside the method, debugging each line. This is useful when you want to get deeper into the code and see how the methods work.
The keyboard shortcut of step into is F11.
3. Step out
As the name suggests, step out will bring you out from the current method that is being debugged. When using step out, the rest statements in the current method will be executed without debugging, and the debugger will return to the method caller. Pressing step out in the main method will continue the program without debugging until the end of the program unless the debugger meets another breakpoint.
The keyboard shortcut of step out is Shift + F11.
Although debug mode provides more features that are not mentioned in this post, what you have read is sufficient to understand and start practice debugging with debug mode supplied by Visual Studio. As you will be executing the program slowly, line by line, you will easily spot bugs. Additionally, being able to see the values of variables currently stored in the memory is also useful to ensure that the variables are storing expected values.
This article is intended for new programmers. I have seen a lot of new programmers do not utilise the debugger available in the IDE, and many programming tutorials on the internet do not talk about debugging. Beginners need to train their debugging skills as it is necessary to be a good developer. That is it for this article, feel free to correct me if anything is wrong.
Top comments (1)
Great article!
This is very helpful for beginners or even experienced programmers that does not use the debugging tools.