简体   繁体   English

真的需要 Sonar Qube 中令人困惑的三元规则吗?

[英]Is Confusing ternary Rule in Sonar Qube really needed?

I write my java code我写我的java代码

if(x !=null){
    // Do something
}
else {
    // Do something
}

Then sonar Qube gives violation.然后声纳 Qube 给出违规。 Is the rule really needed for Confusing ternary?混淆三元真的需要规则吗?

According to the reference link you provided, the rule states:根据您提供的参考链接,规则规定:

In a ternary expression avoid negation in the test.在三元表达式中避免在测试中否定。 For example, rephrase: "(x != y) ? diff : same" as: "(x == y) ? same : diff".例如,改写:“(x != y) ? diff : same”为:“(x == y) ? same : diff”。 Consistent use of this rule makes the code easier to read.始终如一地使用此规则使代码更易于阅读。 Also, this resolves trivial ordering problems, such as "does the error case go first?"此外,这解决了微不足道的排序问题,例如“错误案例是否先行?” or "does the common case go first?".或“常见情况先行吗?”。

It might be a bit confusing that the rule is named "confusing ternary rule" because what you have is a regular if-else construct, not a ternary expression.将该规则命名为“令人困惑的三元规则”可能有点令人困惑,因为您拥有的是一个常规的 if-else 结构,而不是一个三元表达式。 However, since ?: can be thought of as shorthand for an if-else statement, you can also apply the rule here.但是,由于?:可以被认为是 if-else 语句的简写,您也可以在此处应用该规则。

Does it makes sense?有道理吗? Personally, I think absolutely.就个人而言,我认为绝对。 But if it doesn't apply to your (or your company's) coding style and, therefore, you get many violations, I'd rather remove—or adapt—the rule.但是,如果它不适用于您(或您公司)的编码风格,因此,您会遇到很多违规行为,我宁愿删除或调整该规则。 As the reference says: consistency is important.正如参考文献所说:一致性很重要。

This is required cause it's unnecessary causes confusion in first look.这是必需的,因为它是不必要的,乍一看会引起混淆。

eg例如

if(reporters username is not oliver)
   say it's stackoverflow's concern
else 
   say it's oliver's concern

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM