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

前端 未结 6 1280
天命终不由人
天命终不由人 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: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"

提交回复
热议问题