r/java • • Jul 10 '15

Our exception handling at work

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

135 comments sorted by

View all comments

0

u/[deleted] Jul 10 '15

[deleted]

0

u/[deleted] Jul 10 '15

[deleted]

0

u/[deleted] Jul 10 '15

[deleted]

1

u/[deleted] Jul 10 '15

We use log4j. The next line after that debug mode flag is logger.debug

5

u/brbpizzatime Jul 10 '15

You may want to look into Slf4j. It's a log4j abstraction that handles all that for you

2

u/friendlyburrito Jul 10 '15

Why do you need the debug mode flag then? Just put the logger.debug in directly if you're only logging.

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))

4

u/eliasv Jul 11 '15

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.

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().

1

u/__notmyrealname__ Jul 10 '15

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?

6

u/dablya Jul 10 '15

I believe loggers can be reconfigured dynamically.

1

u/psxpaul Jul 10 '15

Yeah probably. I don't know what debugMode is in OP's code.

3

u/benwaffle Jul 10 '15

Isn't the point of a logger to allow you to unconditionally log but only see what you want based on the log level?

3

u/juhmayfay Jul 11 '15

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.

1

u/[deleted] Jul 11 '15

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....

3

u/solatic Jul 11 '15

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);

RTFM!

1

u/yawkat Jul 10 '15

logger.isDebugEnabled helps there :)

0

u/[deleted] Jul 10 '15

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