开发者

Commenting out System.out.println

Do System.out.println(..开发者_开发百科.) calls pose any effect if left in BlackBerry code or any other programming language?

When removed, the compilation time may be reduced, but is there any particular other reason to remove them?


There are a couple of things you need to know before using System.out.println() on Blackberry:

  1. Once you print out something to the standard output any person that has your application installed on the device will be able to see them. All they need to do is to attach the device to the simulator and run in debug mode. So make sure you do not print out anything sensitive such as passwords, class names etc. in the released application.

  2. The performance overhead that the System.out.println() itself makes is minimal, especially when the output stream is not attached to anything (i.e. Device is not connected and not in debug mode).

I myself rather use Blackberry preprocessor to be able to disable all logs before making a release. For this reason I define a logging directive LOGGING and then in my code:

//#ifdef LOGGING
System.out.println("LOGGING is enabled");
//#endif

For more on how to use preprocessors in Blackberry Eclipse plugin see this.


I prefer to use a flag to disable sysouts. Sysouts are really slow if you use them a lot, eg. in loops.


If you don't intend to use the output for anything like debugging ect. then it's best to take it out. Your program will only run as fast as the line can be output so in theory the less system.out line you have the faster the process will be.

Hope this helps.


Runtime might be also reduced, as the statements are actually executed - even if the user doesn't see the output on the screen. If you're using a lot of these (e.g. in tight loops) or you're passing to them Objects with expensive toString() methods, the useless output may be slowing you down.

Also, if you're passing String as an argument, those will take some space in bytecode and in memory. You on your souped-up machine with 173 PB of RAM may not care, but there are resource-constrained systems (such as mobile devices).


You should be able to use Ant to preprocess these lines out of your source code. (Make sure that none of them have side-effects!)


I don't know specifically about Blackberry, but if your program is writing to an unknown device (i.e. you are not sure where standard out is going), there may be a potential for your app to occasionally/sporadically/inexplicably block momentarily in the attempt to write.


Create your own method, i.e. :

public static void consoleMessage(String msg){
  if(DEBUG_FLAG){
    System.out.println(msg);
  }
}

Then use only this throughout your code. It will save you the time for changing all the lines.


Use something like Log4J instead of system out print statements, it gives you much more flexibility


Keeping System.out statements isn't that bad a thing to do usually. Users might be able to see them so it doesnt always look good in a production environment. A better idea is to use a logging framework such as java.util.logging or log4j. These can be configured to dump output to the console, to a file, a DB, a webservice ...

Keep in mind that just becuase you can't see the output it doesn't mean that no work is being done at runtime. The JVM still has to create a String to pass to system.out (or a log statement) which can take a fair bit of memory/CPU for large/complex objects like collections.


Sysout statements access a synchronized, shared resource, which causes synchronization between threads using it. That can prevent memory consistency bugs in multithreaded programs if there is no other code which enforces synchronization. When the sysout statements are removed, any existing memory consistency bugs in the code may surface for the first time.

For an example of this effect, see: Loop doesn't see changed value without a print statement.


It's not an object and it doesn't have any memory attached to it so there shouldn't be any effect besides the time to run it and compile it. And of course readability maybe lol

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜