RichardWarburton / java-8-lambdas-exercises

Exercises and Answers for Java 8 Lambdas book

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Testing, Debugging and Refactoring - logging example

jmayday opened this issue · comments

commented

Hello,

could you please exaplain once again what's the difference between non-lambda code and it's lambda version? (Chapter 7: Testing, Debugging and Refactoring, site 98)

non-lambda code:

Logger logger = new Logger();
if (logger.isDebugEnabled()) {
 logger.debug("Look at this: " + expensiveOperation());
}

lambda code:

Logger logger = new Logger();
logger.debug(() -> "Look at this: " + expensiveOperation());

What do we win by using the lambda version? We can hide the isDebugEnabled check in non-lambda code as well - don't we?

Hi Jakub,

Yeah, we hide the isDebugEnabled() and keep the responsibility of dealing with logging levels encapsulated in the logger. We also don't have the cost of running expensiveOperation() if we aren't in the debug logging level.

commented

Ahh sure, it's lazy evaluated. Totally missed that. Seems like it takes some time to get the spirit. Thx.