No more System.out.println()

No more System.out.println()

This has been blogged about before by me and other guys, but as I still constantly experience this problem, a little repetition won’t hurt 🙂

In many projects you see the result of System.out.println() statements on the console: statements such as “here I am” or “this should not happen”. These are often left-overs from debugging sessions and flood the log. Although, I personally think there are almost no use cases where you would use sysouts over breakpoints or conditional breakpoints, you cannot just forbid the use of them. In fact, there are good use cases I know for sysouts, e.g. debugging drag and drop where you do not want the debugger to suspend on a drag over. However, polluting the code with sysouts should be a no go. A better solution is to put the sysout statement into the break point’s condition like this:

In this way you do not pollute the code and, if you want to persist your sysouts, you can export the breakpoints.

  • Michel Parisien
    Posted at 14:59, 2011-12-01

    This very thing was shown during the Eclipse democamp in Montreal 2 days ago. There were a few gasps in the crowd.

  • Posted at 19:40, 2011-12-01

    Very cool idea! Thanks for sharing.

  • Posted at 22:42, 2011-12-01

    But be warned – breakpoints like this can cause object initialization, partial object initialization and other nasty issues – you should be sure that your sysout does not affect execution flow, because it will be very difficult to debug!

  • Gilles
    Posted at 18:55, 2011-12-03

    Thanks for the tipp. When taking Krzysztof warning into account its pretty neat thing to do.