Link Details

You don't even have to leave your chair. Login and vote now.
Link 69932 thumbnail
User 245797 avatar

By thoughtworks
via twit88.com
Published: Mar 10 2008 / 10:26

Why we should log.isDebugEnabled when coding...
  • 8
  • 12
  • 1905
  • 773

Comments

Add your comment
User 236137 avatar

dzonelurker replied ago:

0 votes Vote down Vote up Reply

Oh boy! So we should cover each log statement with an if statement? Nonsense.

User 254602 avatar

Nick Brown replied ago:

0 votes Vote down Vote up Reply

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.

User 232567 avatar

jdave replied ago:

0 votes Vote down Vote up Reply

Did you read the article?

User 184684 avatar

kdavies replied ago:

0 votes Vote down Vote up Reply

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.

User 52357 avatar

pulesen replied ago:

2 votes Vote down Vote up Reply

I agree, that this is premature optimization. Put your work into making the code more readable, instead!

User 218789 avatar

eelmore replied ago:

0 votes Vote down Vote up Reply

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.

User 254602 avatar

Nick Brown replied ago:

0 votes Vote down Vote up Reply

It can't, at least not in Java. By the time the log method is called, its parameters have already been created.

User 239822 avatar

cohagen69 replied ago:

0 votes Vote down Vote up Reply

This is a 20 century hint. Forget it.

User 204561 avatar

Ignacio Coloma replied ago:

0 votes Vote down Vote up Reply

Modern log frameworks (not log4j) use varargs to avoid converting to String and String concatenation prematurely. This is old news.

User 189024 avatar

kieronwilkinson replied ago:

0 votes Vote down Vote up Reply

Even java.util logging supports string parameterisation (albeit, not as nicely as using vargs would).

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.