Built-in string formatting vs string concatenation as logging parameter

前端 未结 4 1999
野性不改
野性不改 2021-01-01 10:42

I\'m using SonarLint that shows me an issue in the following line.

LOGGER.debug(\"Comparing objects: \" + object1 + \" and \" + object2);

S

4条回答
  •  渐次进展
    2021-01-01 11:19

    I believe you have your answer there.

    Concatenation is calculated beforehand the condition check. So if you call your logging framework 10K times conditionally and all of them evaluates to false, you will be concatenating 10K times with no reason.

    Also check this topic. And check Icaro's answer's comments.

    Take a look to StringBuilder too.

提交回复
热议问题