java - Replacing System.err.println in quick debug of short code examples -
i have write short code samples using ide overkill; use gedit. debugging purposes, i've been using quick method dbgm
. google search has thrown similar suggestions:
import java.util.scanner; public class tyle { public tyle() { } /** * @param debugmessage * method takes debugmessage , prints string version * system.err.println */ private static void dbgm(object debugmessage) { system.err.println(debugmessage.tostring()); } /** * @param number */ public static int cube(int i) { int cube; cube=i*i*i; dbgm(cube); //debug message return cube; } public static void main(string [] args){ system.out.print("enter number: "); scanner s = new scanner(system.in); int inp = s.nextint(); tyle.cube(inp); s.close(); } }
this has merits, can search dbgm
, , leave bona fide system.out.println()
calls alone.
but not @ sure if using method practice, when in debug mode. have been thinking of using akin generics, provide type rather specify object
(it feels risky) , depend on tostring()
.
i can use assert
in simplistic example, there can more complex situations during user studies. such if number
based on user input. there can situations trying check parameters giving expected values.
is there way safely write variant of dbgm
method can appropriate descriptor, if example custom object? want keep reusing solution in sort of 'production' environment.
is there right way?
if code samples writing consists of single java files, i'll recommend not using dbmg
method, using system.our/system.err
print methods explicitly. using custom logging abstraction complicate sample code unnecessary details.
however, if:
- others going use code sample in other projects.
- you code sample end in production.
- you code sample bundled in project (using multiple files , dependency management).
then don't reinvent wheel, use logging framework.
Comments
Post a Comment