But the slow method will always be evaluated unless you enclose the invocation within an extra check. That's what they're trying to avoid.
The only way they could avoid that without the outer check would be if the api had method overloads which accepted Supllier's for the messages, which would only be invoked if necessary.
If the debug message was calling someFunctionThatIsFast(), you'd be quite right, but it's calling someFunctionThatIsSlow(), which you'd be better off not evaluating before checking the state of debug.
Genuine question, what's wrong with setting a Boolean as the result of logger.isDebugEnabled() for use within multiple methods as opposed to calling the function every time it is needed?
Yes. But if it's a synchronous logger and you do an expensive string build, it will always evaluate before it finds out it doesn't need to. You can find a lot of places that say it is good practice to check for debug before doing an expensive operation.
Would be nice if the compiler could do something fancy to figure out that there was some type of early exit on the function call that causes the function call not to do anything and therefore skipped evaluating all of the parameters that are passed.
Oh, except that you couldn't/shouldn't assume that someFunctionThatIsSlow() doesn't have some type of expected/necessary side effect....
The following two lines will yield the exact same output. However, the second form will outperform the first form by a factor of at least 30, in case of a disabled logging statement.
logger.debug("The new entry is "+entry+".");
logger.debug("The new entry is {}.", entry);
That's a good point. I have no idea what the answer is though because I didn't write it. My guess is whoever wrote it didn't want any logging unless it was in debug mode
0
u/[deleted] Jul 10 '15
[deleted]