Over a million developers have joined DZone.

Effective Debugging: Conditional Breakpoints

· Agile Zone

Learn more about how DevOps teams must adopt a more agile development process, working in parallel instead of waiting on other teams to finish their components or for resources to become available, brought to you in partnership with CA Technologies.

One 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.



Discover the warning signs of DevOps Dysfunction and learn how to get back on the right track, brought to you in partnership with CA Technologies.

java ,eclipse ,ides

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}