fb-contrib 的误报:SEO_SUBOPTIMAL_EXPRESSION_ORDER?
False postive for fb-contrib:SEO_SUBOPTIMAL_EXPRESSION_ORDER?
SonarQube 认为以下代码违反了 fb-contrib 规则:SEO_SUBOPTIMAL_EXPRESSION_ORDER(注意,代码示例已简化且不符合逻辑):
class Foo {
boolean baz;
boolean foo() {
return bar() && baz==Baz.VALUE; //Violation of fb-contrib:SEO_SUBOPTIMAL_EXPRESSION_ORDER
}
boolean bar() {
return baz == Baz.VALUE_2;
}
}
enum Baz {
VALUE, VALUE2
}
Performance - Method orders expressions in a conditional in a sub optimal way
(fb-contrib:SEO_SUBOPTIMAL_EXPRESSION_ORDER)
This method builds a conditional expression, for example, in an if or while statement where the expressions contain both simple local variable comparisons, as well as comparisons on method calls. The expression orders these so that the method calls come before the simple local variable comparisons. This causes method calls to be executed in conditions when they do not need to be, and thus potentially causes a lot of code to be executed for nothing. By ordering the expressions so that the simple conditions containing local variable conditions are first, you eliminate this waste. This assumes that the method calls do not have side effects. If the method do have side effects, it is probably a better idea to pull these calls out of the condition and execute them first, assigning a value to a local variable. In this way you give a hint that the call may have side effects.
我认为规则实现查看实际表达式内部是合理的,如果内容是值检查则不会触发违规。
这是一个错误还是我遗漏了什么?
您几乎已经给出了问题的答案:
This causes method calls to be executed in conditions when they do not need to be, and thus potentially causes a lot of code to be executed for nothing. By ordering the expressions so that the simple conditions containing local variable conditions are first, you eliminate this waste.
FB-Contrib 希望您转过身来表达:
boolean foo() {
return baz==Baz.VALUE && bar();
}
这样,只有baz==Baz.value
才需要执行bar()
。当然,这是编译器或 JVM 可能会优化的东西,因此可以归结为一个微基准来确定是否确实需要这种预防措施。
但这在语法层面上是有意义的,因为调用方法 通常比值检查更昂贵。所以你不需要查看方法内部来告诉它。无论如何,对编译器/JVM 内联行为的任何猜测都可能是错误的。
SonarQube 认为以下代码违反了 fb-contrib 规则:SEO_SUBOPTIMAL_EXPRESSION_ORDER(注意,代码示例已简化且不符合逻辑):
class Foo {
boolean baz;
boolean foo() {
return bar() && baz==Baz.VALUE; //Violation of fb-contrib:SEO_SUBOPTIMAL_EXPRESSION_ORDER
}
boolean bar() {
return baz == Baz.VALUE_2;
}
}
enum Baz {
VALUE, VALUE2
}
Performance - Method orders expressions in a conditional in a sub optimal way (fb-contrib:SEO_SUBOPTIMAL_EXPRESSION_ORDER)
This method builds a conditional expression, for example, in an if or while statement where the expressions contain both simple local variable comparisons, as well as comparisons on method calls. The expression orders these so that the method calls come before the simple local variable comparisons. This causes method calls to be executed in conditions when they do not need to be, and thus potentially causes a lot of code to be executed for nothing. By ordering the expressions so that the simple conditions containing local variable conditions are first, you eliminate this waste. This assumes that the method calls do not have side effects. If the method do have side effects, it is probably a better idea to pull these calls out of the condition and execute them first, assigning a value to a local variable. In this way you give a hint that the call may have side effects.
我认为规则实现查看实际表达式内部是合理的,如果内容是值检查则不会触发违规。
这是一个错误还是我遗漏了什么?
您几乎已经给出了问题的答案:
This causes method calls to be executed in conditions when they do not need to be, and thus potentially causes a lot of code to be executed for nothing. By ordering the expressions so that the simple conditions containing local variable conditions are first, you eliminate this waste.
FB-Contrib 希望您转过身来表达:
boolean foo() {
return baz==Baz.VALUE && bar();
}
这样,只有baz==Baz.value
才需要执行bar()
。当然,这是编译器或 JVM 可能会优化的东西,因此可以归结为一个微基准来确定是否确实需要这种预防措施。
但这在语法层面上是有意义的,因为调用方法 通常比值检查更昂贵。所以你不需要查看方法内部来告诉它。无论如何,对编译器/JVM 内联行为的任何猜测都可能是错误的。