0

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.

Riza
  • 36
  • 1
  • 5
  • 4
    It's hard to answer your question because the premise isn't true. The else-if is most definitely reachable. Is there maybe some context missing here that might make that true? – Gary Oct 03 '19 at 18:17
  • Questions of readability are inherently opinion-based. Personally, I would write this without the `else`s, but more out of habit than any particular conviction that it is "better". (There is certainly no performance difference) – Andy Turner Oct 03 '19 at 18:27
  • I call the concept "flat code", which only loosely applies here. Basically, rather than _nested_ if statements, you can occasionally pull conditions out so an if statement just returns right away. In this case though, the only code removed is `else`, so it's not really a major issue here. – Rogue Oct 03 '19 at 18:28
  • @Gary there is nothing missing in the code that I have provided. The String may or may not have any value. – Riza Oct 03 '19 at 19:42
  • This question is opinion based. But I'd try to avoid multiple returns in one method – Kirill Oct 03 '19 at 20:13
  • @Gary is correct; your question is meaningless because it is based on a false premise, so I am voting to close it for that reason. You should be able to verify this for yourself by seeing that the compiler is not objecting to your code. Now change `i.isEmpty()` to `3 > 2` and you should see the compiler telling you that you have unreachable code. – skomisa Oct 04 '19 at 04:42
  • @skomisa I know that the code is not unreachable. I have even written JUnit for it. My question was that is using multiple `if` is preferred over `else if` for the above scenario. – Riza Oct 04 '19 at 19:32
  • @Rogue I think this is called Guard Clauses.... – mrossini Oct 05 '19 at 06:54

3 Answers3

1

I think you need to speak to the code reviewer. I’m pretty sure the else if is reachable if the ‘if’ statement returns false

Rui
  • 21
  • 2
  • yes I know that the code is reachable as I have written JUnit for it. My question was whether `if` is preferred over `else if` in the above case? Is there any performance diff? I have already asked the same question to the code reviewer but I won't be able to get an answer from the reviewer for a few days. Thanks – Riza Oct 04 '19 at 20:16
  • Understood @Riza. For your question, in my opinion, there is no much performance differences. I personally do use “else if” in this case, it’s simply a habit. Because it’s checking same string reference. But either way will be fine, both are readable. BTW, do you need to check “null == i” before i.isEmpty() check? Just prevent a NPE. – Rui Oct 04 '19 at 23:13
0

In your code else if(i.equals("something")){ is not unreachable code. When the input was non empty value it will execute the else if block and the input value equals to "something" then the statements inside else if block will execute.

Depending up on business scenario we need to use if elseIf . If we use only if statements every if condition will be evaluate . It takes some time to perform evaluation.

If we know we need to perform some operations in true case and some in false case then better use if elseif ..

Sekhar
  • 61
  • 3
-1

It has anything to do with being reacheable ot not. If is is not empty then the else will be reached.

I think that once it gets inside the first if flow will return then there is no point in the else. In other words else does not add anything therefore it is noise. There are people that prefer having else. Personally I believe without else code is easier to understand.

mrossini
  • 378
  • 3
  • 10
  • mrossini is right, the else is just adding noise. With a brief glance at the code, the `else` implies that control can continue past the first if statement without executing `doSomtehing()`. – Jamie Oct 03 '19 at 18:37
  • No, the else isn't just noise. It would be if the condition was elseif-!empty, but it's not. It only happens if the equality is true. – Gary Oct 04 '19 at 10:33
  • @gary It is total noise. If condition is not true it goes to the next if anyway, whether the else is there or not, therefore it is NOT needed and as such it is noise. "elseif-!empty" is by definition the else clause of "isEmpty"..... – mrossini Oct 04 '19 at 11:38
  • Oh, you meant just the 'else' in 'else if'. I thought you were calling the entire second condition noise. – Gary Oct 04 '19 at 11:48
  • @greg Not the entire else block, just the "else" keyword :) much like this: https://www.refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html – mrossini Oct 04 '19 at 11:57
  • So, as we have a `return` statement in the `if` block the purpose of `else if` is achieved. The `else` is not adding any value to the code. I never thought about it this way. I do prefer having an `else` when I have to evaluate the same variable. I also believe that there will be no performance difference here. Do let me know if there is. I will accept this answer after reading all the responses. Thanks. – Riza Oct 04 '19 at 20:00
  • @Riza No difference as far as I know. I have seen thru the bytecode generated with and without the else and they are exactly the same. I think it goes down to personal preference and I see you have yours. On the other hand you I think code review should not go into this level of detail at all, but that is a discussion for some other time...... – mrossini Oct 05 '19 at 06:44