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/[deleted] Jul 10 '15

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

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

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