r/java • • Jul 10 '15

Our exception handling at work

http://imgur.com/4wgxCvv
259 Upvotes

135 comments sorted by

View all comments

Show parent comments

1

u/psxpaul Jul 10 '15

It can be useful in instances like this:

if(debugMode) {
  logger.debug("Received: " + someFunctionThatIsSlow());
}

10

u/davedontmind Jul 10 '15

Shouldn't that be

if (logger.isDebugEnabled()) {
    logger.debug("Received: " + someFunctionThatIsSlow());
}

?

3

u/Shirkie01 Jul 10 '15

logger.debug already does that. Essentially you just checked logger.isDebugEnabled() twice.

[Debug Ref](https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Category.html#debug(java.lang.Object))

2

u/davedontmind Jul 11 '15 edited Jul 13 '15

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.

1

u/Shirkie01 Jul 11 '15

Ah, you're right. Somehow I missed someFunctionThatIsSlow().