Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Effective Debugging: Conditional Breakpoints

DZone's Guide to

Effective Debugging: Conditional Breakpoints

· Agile Zone
Free Resource

Reduce testing time & get feedback faster through automation. Read the Benefits of Parallel Testing, brought to you in partnership with Sauce Labs.

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.

 

 

The Agile Zone is brought to you in partnership with Sauce Labs. Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure.

Topics:
java ,eclipse ,ides

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

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

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}