You probably know some post of this kind. “n things about debugging” is a popular topic. For a good reason! I guess that I spent about 1 hour per day debugging my applications. That’s quite a lot time. This makes it even more important to know all the handy features, that makes your life easier and helps you to safe some time. And it justifies another post on this topic!
– First Tip: Don’t debug too much!
Crazy statement to start a post about debugging. 😉 But it must be said! Try to split up your complex logic into many isolated units and write Unit-Tests to check the correct behaviour of your code. I see it very often, that people are clicking through large web-applications, filling out multiple forms, switching to different pages in the flow, just to check a calculation logic on the last page and after that implementing big parts of the logic in the debug view. Before starting your tomcat always ask yourself: Is there a way to check the behaviour with a unit test? But you can find tons of good material about good code design out there, from here I will focus on a few eclipse debugging tips, which you maybe don’t know or you have forgotten over the time.
– Breakpoint View : Conditional Breakpoints
Extreme useful if you are interested only in a special constellation of your application. For example if you want to see the 13th run in a loop or you are debugging functionality in an abstract super class and you only want to see one concrete implementation. You can setup the conditions in the breakpoint-view or with the contextmenu on the blue breakpoint-marker next to your code (“Breakpoint Properties”). You can choose to suspend when your code-snippet gets true or when the value of your snippet changes.
– Variables View: Show Logical Structure
If you want to see the values of a Map or a List in the variables view, it’s not always that easy with the default setting of eclipse. If you are using a HashMap for example, you have to click through the physical entries and you are confronted with implementation details of a HashMap. But there is a small button above the variables – “Show Logical Structure”. Very handy, especially if you don’t have meaningful toString()-methods for the objects in your structure. My boss showed me this feature a few weeks ago. You know, he is the guy who is working with PowerPoint or Excel the most time. What a shame for a developer like me 😉Without “Show Logical Structure”
With “Show Logical Structure”
– Variables View: Change Value…
Instead of restarting your debug session with some slightly changed input data, let’s say entered in a web-form – you can also change the values of your variables directly during debugging. Sometimes you can safe some time and sometimes you can simulate some strange behaviour with that feature a bit easier.
– Display View
Do you know the “Display View”? You can activate it during debugging via “Window” -> “Show View” -> “Display”. Now there should be a blank new view in your Eclipse. You can use this view to enter and evaluate new code. The code is executed within the context of the current debugging positions, which means that you can use all your variables and even content assist. To execute your code, just mark it and use the context-menu or CTRL+U (execute) or CTRL+SHIFT+I (inspect).
– Navigation: Drop to Frame
I think everybody knows “Step Into”, “Step over” and maybe “Step return”. That are the basics to navigate through your code while you are debugging. I want to name 2 further ways to navigate, which I like very much. The first is “Drop to Frame”. With that feature you have the possibility to go back in time 😉 You can simply go to a point in your java stackframe where you have been before. It happens to me quite often, that I am debugging and then missing the point where I have to pay attention. With the “Drop to Frame”-Feature I can then run the code again very simple.
– Navigation: Step into Selection
The second one is “Step into Selection”. This is a pretty easy one, but not many people are using it. For that, you just have to press Ctrl+Alt and click on a method name, where you want to go. Very handy, very fast. Compared to the usual “Step Into” this is much better for example if you want to enter a method with many parameters and you one step trough all the parameter evaluations. “Run to line” is also a nice feature. Just place your cursor in front of the line where you want to stop and hit “CTRL+R”.
– Navigation: Use your keyboard
You are faster, if you avoid to use the mouse. You should know at least the following Shortcuts:
- F5 – “Step Into”
- F6 – “Step Over”
- F7 – “Step Return”
- F8 – “Resume”
- Ctrl+Shift+B – “Toggle Breakpoint”
- Ctrl+Shift+I – “Inspect”
– Breakpoint View: Watchpoints
What the hell is changing this variable?! Sometimes it is usefull to create a watchpoint. Then the debugger stops, whenever the watched field is changed or just read – you can configure that. Just doubleclick on a field, then you can see the watchpoint in the Breakpoint View and edit the properties. You can even configure a hit count, means that the debugger only stops, when the entered hit count is reached. This also works for usual breakpoints.
– Human Readable Objects
The Variables View is using the toString-Method of your objects to display the value. Because of this it is very, very helpful
to provide good toString-implementations. The javadoc of the default-toString-implementation in java.lang.Object recommends the same:
* Returns a string representation of the object. In general, the
* <code>toString</code> method returns a string that
* "textually represents" this object. The result should
* be a concise but informative representation that is easy for a
* person to read.
* It is recommended that all subclasses override this method.
You can have a look at the ToStringBuilder in commons-lang. It provides some functionality to write – quote from the javadoc – “good and consistent” toString-methods.Default-ToString
Example from ToStringBuilder – Default
Example from ToStringBuilder – Multiline-Style
If you cannot modify the toString-implementation, for example if you are working with frameworks or you have to use a foreign API, it might be an option to create an “Detail Formatter” in Eclipse. To do that, you have to right click an object in the variables view and click on “New Detail Formatter…”. Then you can provide some code to display this type of Object in the future.