Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...