Is there any appreciable difference between if and if-else?

前端 未结 9 893
执笔经年
执笔经年 2021-01-12 06:55

Given the following code snippets, is there any appreciable difference?

public boolean foo(int input) {
   if(input > 10) {
       doStuff();
       retur         


        
9条回答
  •  攒了一身酷
    2021-01-12 07:43

    • In the first:

      somebody eventually, by some strange reason and when you're not looking will add some add statement that will make this method fail under certain strange conditions, everybody ( or worst, one single person ) will spend 4 hrs. watching the source code and debugging the application to finally found there was something in the middle.

    • The second is definitely better, not only it prevents this scenario, but also helps to clearly state , it this or this other no more.

      If all the code we write within an if where 10 lines long at most, this wouldn't matter really, but unfortunately that's not the case, there exists other programmers which by some reason think that a if body should be > 200 lines long... anyway.

    • I don't like the third, it forces me to look for the return variable, and it's easier to find the return keyword

    About speed performance, they are ( almost ) identical. Don't worry about that.

提交回复
热议问题