Effective Debugging: Conditional Breakpoints
Join the DZone community and get the full member experience.
Join For FreeOne of the most important developer activities is debugging. In my college days, we were forced to use simple text editors for our software development, so I started out using print statements to see where my code was going wrong. These days, we have the comfort of IDEs, but debugging remains one of those talents that you get more efficient at with experience.
The best feature I have seen in both Eclipse and NetBeans is the conditional breakpoint concept. The idea is simple, take your breakpoint, and type in a condition for the breakpoint to stop at. For example if I have the following code:
for(Employee emp: employees){ if(emp.getType() == Employee.MANAGER) { addToManagers(emp); }}
Let's say that I want to see the values of all the variables when the employee's getType value is Employee.DEVELOPER, and step through to make sure that we don't enter the if statement block.
In Eclipse, you can set the breakpoint as normal on the if statement, right click on the breakpoint and you'll see the following menu:
You can now set a condition for your breakpoint in the resulting dialog by selecting the Enable Condition checkbox:
As you type in the condition you get full content assist. You can also change what this condition means - whether to stop when it's true, or when it changes since the last iteration.
In NetBeans, it's much the same. You still get the properties on right-click of the breakpoint:
The dialog is also similar with conditions and hit count, with content assist:
Things like this can really help speed up your debugging - when you know a particular value has caused your program to fail, you can get right to the heart of the matter.
Opinions expressed by DZone contributors are their own.
Comments