Programming in VS Code is not easy. Even the smallest mistakes can cause huge problems and hamper your projects. To overcome these obstacles, you need a reliable debugging technique in your toolkit. This is where breakpoints come in.
Breakpoints are used whenever you want to pause your debugger execution. They allow you to examine the state of your code variables and perform many other crucial tasks to get your programming back on track. This is why understanding how to use breakpoints in VS Code is a must.
In this entry, we'll give you a detailed guide on how to use VS Code breakpoints. You'll learn about the most popular types and find out how they can help your development.
How to Use Breakpoints in VS Code
Breakpoints in VS Code can be placed in any executable code. This works for method signatures, declarations for a class or namespace, and even for variable declarations if there are no getters/setters or assignments.
To set a breakpoint in your source code, follow these steps:
- Click on the left margin or press the F9 key next to the line you want to break.
- Run the code or press F5 (“Continue”).
- Your code will now stop before the marked execution. The breakpoint will appear as a red dot inside your left margin.
By default, lines of running code and current breakpoints are automatically highlighted for most programming languages, including C#. If you are working in C++, you can enable highlighting as follows:
- Go to "Debug" or "Tools".
- Choose "Options", followed by "Debugging".
- Select the following command: “Highlight entire source line for current statement and breakpoint. "
Once the debugger stops at your breakpoints, you can inspect the current state of your application. The data you can view includes call stacks and variable values.
As for color, breakpoints are usually colored red if you are working within the margin of your editor. Disabled breakpoints are represented by a solid gray circle, while a gray hollow circle indicates a breakpoint that cannot be saved. The latter may also apply if you edit the source while your debugging sessions without live editing support are in progress.
Here are some more notable breakpoint commands:
What are logpoints in VS Code?
Log points are another useful variation of breakpoints. Instead of penetrating your debugger, they log messages to your console and serve as temporary trace statements in your programming language. Moreover, they do not interrupt the execution of the code.
Logpoints can be a great injection device when you're debugging a production server that can't be stopped or paused. They appear as diamond-shaped icons and feature plain text. However, they can also be accompanied by expressions evaluated with braces.
Like standard breakpoints, logpoints can be toggled on and off. You can also control them with a number of moves or a condition.
Additionally, while they are supported by the built-in Node.js debugger, they can also be applied through other debugging platforms. The list includes both Java and Python extensions.
How to use conditional breakpoints in VS Code?
One of the most powerful features of VS Code is the ability to insert conditions based on hit count, expressions, or combinations of the two:
• “Hit Count” – The “Hit Count” function dictates the number of times you must hit your breakpoint before it interrupts code execution. The syntax of this expression and the respect of the hit count depends on your debugger extension.
• “Expression Condition” – The code hits this breakpoint each time your expression evaluates to “True”.
You can add hit counts and conditions when creating source breakpoints with your "Add Conditional Breakpoint" option. Alternatively, these features can be accessed when editing existing breakpoints via the "Edit Condition" function. Either way, you should see a text box and a menu for entering their expressions. You can also edit conditions using your context menu or the "Edit Condition" window.
Additionally, VS Code supports hit count and conditions for 'Exception' and 'Function' breakpoints. If your debugger does not support conditional breakpoints, the "Edit condition" and "Add conditional breakpoint" options will not be available.
What are inline breakpoints in VS Code?
Inline breakpoints are only hit when code execution arrives at the column connected to your inline breakpoint. They are especially useful when debugging minified code that contains multiple single-line statements.
To set inline breakpoints, you can use the “Shift + F9” key combination. Another option is to access the “Context” menu while you are in a debugging session. They will be displayed in the edit window.
The "Context" menu also allows you to edit multiple breakpoints on a single line.
What are function breakpoints in VS Code?
Rather than placing a breakpoint directly in your source code, you can create one by designating a function name. This feature works great for unavailable sources with a familiar function name.
Here's how to create a function breakpoint:
1. Tap the “+” symbol in your “Breakpoints” header.
2. Enter a function name.
3. This will create a function breakpoint and it will be represented by a red triangle.
What are data breakpoints in VS Code?
Some debuggers also support data breakpoints. They can be activated via the "Variables" window and are affected when the value of the variable changes. Breakpoints appear as red hexagons in the "Breakpoints" menu.
Gateway to many possibilities
Breakpoints in VS Code can be used in a variety of ways, opening the door to nearly endless possibilities when debugging your code. With all the types of breakpoints we described above at your disposal, you will easily observe the behavior of your lines and facilitate the debugging process. Best of all, most of them can be activated quickly, and each of them is clearly represented to further speed up your coding efforts.
Have you tried using breakpoints in VS Code? What type of breakpoint do you use most often? Have you already activated an Object ID? Let us know in the comments section below.