gpt4 book ai didi

java - 循环中字符串连接的误报 SonarQube 违规

转载 作者:行者123 更新时间:2023-12-01 13:40:38 28 4
gpt4 key购买 nike

我有一些看起来像这样的代码(我用通用变量替换了我的业务变量):

Map<String, String> map = new HashMap<String, String>();
for (int i = 1; i < 10; i++) {
String suffix1 = retrieveValue1(i);
String suffix2 = retrieveValue2(i);
String tag = "prefix";
if (suffix1 != null) {
tag += suffix1;
}
else {
tag += suffix2;
}
map.put(tag.toUpperCase(), "on");
}

让我感到困扰的是我收到以下 SonarQube 违规:

Performance - Method concatenates strings using + in a loop



在我看来,这是一个误报(因为这里的 String 没有真正的循环),但我想先仔细检查一下。

我在我的 friend Google 上找不到任何类似的案例。

这是误报,还是我的循环中有真正的性能损失?

最佳答案

是的,SonarQube 可能对 += 的使用感到困惑。循环内。
String tag = "prefix";在循环内创建,因此 for 内没有字符串连接循环,从技术上讲,警告是误报。

请注意,您仍然可以使用 StringBuilder附加标签的两个部分,但您必须衡量是否有必要。

关于java - 循环中字符串连接的误报 SonarQube 违规,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34288341/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com