java 用一个 return 语句替换这个 if-then-else 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45817581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:55:25  来源:igfitidea点击:

Replace this if-then-else statement by a single return statement

javasonarqube

提问by Eran

While solving sonarQube issue i face the below warning,does any one tell me how to overcome this warning

在解决 sonarQube 问题时,我面临以下警告,有没有人告诉我如何克服此警告

Method:-

方法:-

@Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Division other = (Division) obj;

        if (divisionId != other.divisionId)
        //getting warning for above if condition

            return false;
        return true;
    }

Warning :

警告 :

Replace this if-then-else statement by a single return statement.

用一个 return 语句替换这个 if-then-else 语句。

Description:-

描述:-

Return of boolean literal statements wrapped into if-then-else ones should be simplified.

应该简化包装到 if-then-else 语句中的布尔文字语句的返回。

回答by Eran

Well, you can replace:

好吧,你可以替换:

if (divisionId != other.divisionId)
    return false;
return true;

with the equivalent:

与等效:

return divisionId == other.divisionId;

This will return falseif divisionId != other.divisionIdand trueotherwise.

false如果divisionId != other.divisionIdtrue否则,这将返回。

回答by Nadun Priyankarage

I received a similar kind of warning message when using sonarlint "Return of boolean expressions should not be wrapped into an "if-then-else" statement" this was my code previously,

我在使用 sonarlint 时收到了类似的警告消息“布尔表达式的返回不应包含在“if-then-else”语句中”这是我以前的代码,

if (val.isEmpty()) {
    switchCompat.setChecked( false );
} else {
    switchCompat.setChecked( true );
}

now i changed it to,

现在我把它改成,

boolean checked = val.isEmpty();
switchCompat.setChecked( checked );

According this question, it is similar to,

根据这个问题,它类似于,

@Override
public boolean equals(Object obj) {
    Division other = (Division) obj;
    if (this == obj)
        return true;
    else if (obj == null)
        return false;
    else if (getClass() != obj.getClass())
        return false;
    else if (divisionId != other.divisionId)
        return false;
    else
        return true;
}

Similarly, it can be resolve like this,

同样的,也可以这样解析,

@Override
public boolean equals(Object obj) {
    boolean success;
    Division other = (Division) obj;

    if (this == obj)
        success = true;
    else if (obj == null)
        success = false;
    else if (getClass() != obj.getClass())
        success = false;
    else if (divisionId != other.divisionId)
        success = false;
    else
        success = true;
    return success;
}

回答by Yash

Sonar Qube Rule:squid:S1126- Return boolean expressions instead of boolean literal

声纳 Qube 规则:squid:S1126-返回布尔表达式而不是布尔文字

In SonarQube, analyzers contribute rules which are executed on source code to generate issues. There are four types of rules:

在 SonarQube 中,分析器提供在源代码上执行以生成问题的规则。有四种types of rules

  • Code Smell (Maintainability domain)
  • Bug (Reliability domain)
  • Vulnerability (Security domain)
  • Security Hotspot (Security domain)
  • 代码气味(可维护领域)
  • 错误(可靠性域)
  • 漏洞(安全域)
  • 安全热点(安全域)
Noncompliant Code Example      |  Compliant Solution
---------------------------    |  ----------------------------
boolean foo(Object param) {    |  boolean foo(Object param) {
    /*Some Condition*/         |    boolean expression = false;
    if(param == null) {        |    if(param != null) { // param == null - squid:S4165
        return true;           |        //expression = true; //(squid:S4165)
    }                          |    //} else {
                               |        if(/**/) { // Compliant
    if(/**/){/* Noncompliant*/ |            expression = true; 
        return true;           |        } else if(/**/) {
    } else if(/**/) {          |            expression = true;      
        return true;           |        } else if(/**/) { // squid:S1871
    } else if(/**/) {          |            expression = true;      
        return true;           |        } else { // To avoid else.
    }                          |            expression = false;
    return false;              |        }
}                              |    }
                               |    return expression;
                               |  }

squid:S1871- Two branches in a conditional structure should not have exactly the same implementation: When multiple else if() { }same code inside the block to overcome this problem above we use extra else {}block with different implementation.

squid:S1871- 条件结构中的两个分支不应具有完全相同的实现:当else if() { }块内有多个相同的代码来解决上述问题时,我们使用else {}具有不同实现的额外块。



SonarSourcerules, making Code Analyzers - Quality software comes from quality code

SonarSource规则,制作代码分析器 - 优质软件来自优质代码

  • SonarQubeContinuous Code Quality - Analyze code in your, on-premise CI. For Online Use SonarQube as a Service
  • Use Sonarlintwhich Catches the issues on the fly, in your IDE.
  • SonarQube连续代码质量 - 分析本地 CI 中的代码。在线使用 SonarQube即服务
  • 使用Sonarlint,它可以在您的 IDE 中即时捕获问题。

See also:

也可以看看:

回答by St-rm

Not completely sure of your intent for the if-statements that return false but it seems as if you could simply always return false unless "this == obj".

不完全确定您对返回 false 的 if 语句的意图,但似乎您总是可以简单地返回 false,除非“this == obj”。

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    else
        return false;
}

This same thing could be accomplished with one line

同样的事情可以用一行来完成

@Override
public boolean equals(Object obj) {
    return this == obj;
}

回答by Usagi Miyamoto

Try something like this:

尝试这样的事情:

return null != obj && this == obj || getClass() == obj.getClass() &&
       this.divisionId == ((Division) obj).divisionId;

回答by Sai Achyuth

This must work :

这必须工作:

return this == obj
        ? true
        : obj == null
            ? false 
            : getClass() != obj.getClass()
                ? false
                : divisionId != ((Division)obj).divisionId
                    ? false : true