我正在使用SonarLint向我显示以下行中的问题.
LOGGER.debug("Comparing objects: " + object1 + " and " + object2);
Run Code Online (Sandbox Code Playgroud)
附注:包含此行的方法可能会经常调用.
这个问题的描述是
"先决条件"和记录参数不应该要求评估(鱿鱼:S2629)
将需要进一步评估的消息参数传递到Guava com.google.common.base.Preconditions检查可能会导致性能下降.这是因为无论是否需要它们,必须在实际调用方法之前解析每个参数.
类似地,将连接的字符串传递给日志记录方法也会导致不必要的性能损失,因为每次调用该方法时都会执行连接,无论日志级别是否足够低以显示消息.
相反,您应该构造代码以将静态或预先计算的值传递到Preconditions条件检查和记录调用.
具体来说,应该使用内置字符串格式而不是字符串连接,如果消息是方法调用的结果,那么应该跳过前提条件,并且应该有条件地抛出相关的异常.
不合规的代码示例
Run Code Online (Sandbox Code Playgroud)logger.log(Level.DEBUG, "Something went wrong: " + message); // Noncompliant; string concatenation performed even when log level too high to show DEBUG messages LOG.error("Unable to open file " + csvPath, e); // Noncompliant Preconditions.checkState(a > 0, "Arg must be positive, but got " + a); // Noncompliant. String concatenation performed even when a > 0 Preconditions.checkState(condition, formatMessage()); //Noncompliant. …
我有以下类使用以下方法.
public class MyClass {
public static <T> T getSomeThing(final int id, final java.lang.reflect.Type type, final Map<Integer, String> someThings) {
final String aThing = someThings.get(id);
if (aThing == null || aThing.isEmpty()) {
return null;
}
return GsonHelper.GSON.fromJson(aThing, type);
}
}
Run Code Online (Sandbox Code Playgroud)
GsonHelper为我提供了一些帮助 com.google.gson.GsonBuilder
public class GsonHelper {
public static final com.google.gson.Gson GSON = getGsonBuilder().create();
public static GsonBuilder getGsonBuilder() {
return new GsonBuilder().setPrettyPrinting()
.enableComplexMapKeySerialization()
.registerTypeAdapter(new com.google.gson.reflect.TypeToken.TypeToken<byte[]>() {
// no body
}.getType(), new Base64TypeAdapter())
.registerTypeHierarchyAdapter(Date.class, new DateTypeAdapter())
.registerTypeHierarchyAdapter(Pattern.class, new PatternTypeAdapter())
.registerTypeAdapterFactory(new ListTypeAdapterFactory())
.registerTypeAdapterFactory(new …Run Code Online (Sandbox Code Playgroud) 我开发了自己的简单 discord bot(使用 discord.js)并将其部署到节点服务器。一切正常。
现在我想为它添加更多功能。在开发过程中,我想在本地进行测试(当然)。
问题是:我可以在本地运行我的机器人并对其进行测试而不中断我当前正在运行的机器人吗?
如果不是:
我需要另一个机器人/令牌实例吗?
如何在不中断当前运行的机器人的情况下测试我的机器人?