You don't even have to leave your chair. Login and vote now.
By thoughtworks
via twit88.com
Published: Mar 10 2008 / 10:26
By thoughtworks
via twit88.com
Published: Mar 10 2008 / 10:26
Comments
dzonelurker replied ago:
Oh boy! So we should cover each log statement with an if statement? Nonsense.
Nick Brown replied ago:
Yes, you should. Sure, it would be nice if there were a way in Java to have it not create the parameters in the method unless needed like a Lisp macro (actually there might be a way with aspect oriented programming), but all the unnecessary strings you create can have a significant performance impact, especially when methods called to create the string (such as the toString() method of any objects that are appended in there) take non-trivial times. The fact that it looks ugly as sin doesn't make it nonsense.
jdave replied ago:
Did you read the article?
kdavies replied ago:
There are tons of articles on premature optimization. Look them up. If you have a debug statement that is going to take a lot of time to construct, then sure, wrap it in a 'if' statement. Otherwise you are just making you code harder to read and maintain for an optimization that actually makes no useful difference whatsoever.
pulesen replied ago:
I agree, that this is premature optimization. Put your work into making the code more readable, instead!
eelmore replied ago:
The responsibility of checking if debug is enabled DEFINITELY belongs to the log object and NOT the calling object. Doing as the author suggests is the norm for 1987, but that doesn't make it a good idea.
Nick Brown replied ago:
It can't, at least not in Java. By the time the log method is called, its parameters have already been created.
cohagen69 replied ago:
This is a 20 century hint. Forget it.
Ignacio Coloma replied ago:
Modern log frameworks (not log4j) use varargs to avoid converting to String and String concatenation prematurely. This is old news.
kieronwilkinson replied ago:
Even java.util logging supports string parameterisation (albeit, not as nicely as using vargs would).
Voters For This Link (8)
Voters Against This Link (12)