jeudi 3 octobre 2019

Using multiple if statements instead of else if when each statement return something

I had written a piece of code that looks something like this.

public boolean check(String i){
        if(i.isEmpty()){
            doSomething();
            return true;
        }
        else if(i.equals("something")){
            doSomethingElse();
            return true;
        }   
        return false;
    }

During the code review, I got a comment to remove the "else if" block as the first "if" block is having a return statement which makes the other block unreachable.

So I was curious if there is any advantage of using multiple "if" statements over "else if" when each block is returning something. Does the code performance or readability improve by using multiple if statements over "else if"? Why multiple "if" is prefered over "else if" in this case? I know functionality wise they are the same.

Aucun commentaire:

Enregistrer un commentaire