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

2020-04-09 19:54发布

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

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.

Description:-

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

6条回答
来,给爷笑一个
2楼-- · 2020-04-09 20:05

Try something like this:

return null != obj && this == obj || getClass() == obj.getClass() &&
       this.divisionId == ((Division) obj).divisionId;
查看更多
放荡不羁爱自由
3楼-- · 2020-04-09 20:06

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".

@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;
}
查看更多
混吃等死
4楼-- · 2020-04-09 20:09

Well, you can replace:

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

with the equivalent:

return divisionId == other.divisionId;

This will return false if divisionId != other.divisionId and true otherwise.

查看更多
在下西门庆
5楼-- · 2020-04-09 20:09

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

In SonarQube, analyzers contribute rules which are executed on source code to generate issues. There are four 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.


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

  • SonarQube Continuous Code Quality - Analyze code in your, on-premise CI. For Online Use SonarQube as a Service
  • Use Sonarlint which Catches the issues on the fly, in your IDE.

See also:

查看更多
家丑人穷心不美
6楼-- · 2020-04-09 20:09

This must work :

return this == obj
        ? true
        : obj == null
            ? false 
            : getClass() != obj.getClass()
                ? false
                : divisionId != ((Division)obj).divisionId
                    ? false : true
查看更多
贪生不怕死
7楼-- · 2020-04-09 20:20

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,

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;
}
查看更多
登录 后发表回答