Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SonarQube: Invoke method(s) only conditionally

The following part of code raises a major bug at SonarQube : "Invoke method(s) only conditionally."
How am I supposed to fix this?

if(us != null){     logger.info("Log this: {}", us.toString()); } 
like image 709
Olezt Avatar asked Jun 02 '17 08:06

Olezt


2 Answers

The call to us.toString() is redundant, toString() method will be called regardless the configured log level. You should pass only us as an argument to info without an if statement.

logger.info("Log this: {}", us); 
like image 98
Tibor Blenessy Avatar answered Sep 23 '22 12:09

Tibor Blenessy


As stated at the comments of the question, another working answer is:

if(logger.isInfoEnabled() && us != null){     logger.info("Log this: {}", us.toString()); } 
like image 38
Olezt Avatar answered Sep 24 '22 12:09

Olezt