A Java Debugging Template

Revision en1, by skittles1412, 2020-08-02 22:10:06

Hello CodeForces,
due to the lack of a debugging template in Java (and really just any template in Java), I have decided to write my own template. To use it, add -DLOCAL to your VM Parameters or change it to LOCAL = System.getProperty("ONLINE_JUDGE")==null; for CodeForces. Then call dbg(what you want to print) to print the "what you want to print" part to stderr.

To be more exact, when calling dbg(), It will print "Line #%d: [%s]" to stderr, where %d is the line number where you called dbg() and %s is the parameters, separated by commas. For each parameter, if it is able to be iterated using a for each loop (e.g. an array or some class implementing Iterable), it will be printed as {%s} where %s is each element printed, recursively, separated by commas. Otherwise, the .toString() method will be used.

Unfortunately, there is no way to get the names of the variables passed into the parameters of a method in Java, so I'd recommend calling dbg("variable_name", variable) to make it easier to read.

The code is not very clean (mainly due to the fact that arrays can't be cast to Iterable and because primitives can't be used in parametized methods). Please comment below if you have any suggestions or additions to the template (e.g. a class which this dbg method fails to print properly). I'll try to keep the code updated to the best of my ability.

Code

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English skittles1412 2020-08-09 07:07:42 0 (published)
en2 English skittles1412 2020-08-09 06:40:37 301 (saved to drafts)
en1 English skittles1412 2020-08-02 22:10:06 3415 Initial revision (published)