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

前端 未结 6 1197
天命终不由人
天命终不由人 2020-12-21 01:13

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

Method:-

@Override
    public boolean equals         


        
相关标签:
6条回答
  • 2020-12-21 01:21

    Try something like this:

    return null != obj && this == obj || getClass() == obj.getClass() &&
           this.divisionId == ((Division) obj).divisionId;
    
    0 讨论(0)
  • 2020-12-21 01:29

    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;
    }
    
    0 讨论(0)
  • 2020-12-21 01:30

    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:

    • Useless "if(true) {...}" and "if(false){...}" blocks should be removed
    • Change this condition so that it does not always evaluate to "false"
    0 讨论(0)
  • 2020-12-21 01:33

    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.

    0 讨论(0)
  • 2020-12-21 01:41

    This must work :

    return this == obj
            ? true
            : obj == null
                ? false 
                : getClass() != obj.getClass()
                    ? false
                    : divisionId != ((Division)obj).divisionId
                        ? false : true
    
    0 讨论(0)
  • 2020-12-21 01:42

    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;
    }
    
    0 讨论(0)
提交回复
热议问题