Java: includes other here faster? Or is it better?

In situations such as the one in the java fragment below, is it faster or better to use the return false environment with the else block compared to it only after the else block?

public boolean test(boolean var){ if (var == true){ return true; } return false; } 

compared with

 public boolean test(boolean var){ if (var == true){ return true; }else{ return false; } } 

Obviously, this is an extremely simple example, which can be reduced to one line, but my question is whether there is jvm optimization that makes it more efficient to use the else block in the second fragment or if it is better practiced since both fragments are logically equivalent. So far, I have used the first snippet simply because there is a bit less code.

+4
source share
6 answers

I compiled both examples, and the byte codes received are identical:

  public boolean test(boolean var); 0 iload_1 [var] 1 ifeq 6 4 iconst_1 5 ireturn 6 iconst_0 7 ireturn Line numbers: [pc: 0, line: 5] [pc: 4, line: 6] [pc: 6, line: 8] Local variable table: [pc: 0, pc: 8] local: this index: 0 type: scratch.Else1 [pc: 0, pc: 8] local: var index: 1 type: boolean public boolean test(boolean var); 0 iload_1 [var] 1 ifeq 6 4 iconst_1 5 ireturn 6 iconst_0 7 ireturn Line numbers: [pc: 0, line: 5] [pc: 4, line: 6] [pc: 6, line: 8] Local variable table: [pc: 0, pc: 8] local: this index: 0 type: scratch.Else2 [pc: 0, pc: 8] local: var index: 1 type: boolean 

How to use the one you should use? In this contrived example, the answer sounds NEVER. The best code that matches the behavior is as follows:

 public boolean test(boolean var){ return var; } 

Given the choice of "if {return} else {return}" vs. "if {return} return", my answer is usually the last, because I prefer less indentation. But I think it really depends on how much code is in each branch. More than another code points to the last, more if the code points to the first.

+13
source

They are exactly the same. Ofen with such small things as modern compilers will optimize for you, Javac does it. Consider the following class:

 public class IsElseFaster { public boolean test1(boolean var) { if (var == true) { return true; } return false; } public boolean test2(boolean var) { if (var == true) { return true; } else { return false; } } } 

Copy and paste into your favorite editor, compile it with javac . Then run javap -c IsElseFaster You will get the following result:

 public boolean test1(boolean); Code: 0: iload_1 1: iconst_1 2: if_icmpne 7 5: iconst_1 6: ireturn 7: iconst_0 8: ireturn public boolean test2(boolean); Code: 0: iload_1 1: iconst_1 2: if_icmpne 7 5: iconst_1 6: ireturn 7: iconst_0 8: ireturn 

As you can see, by the time you get into the .class file, the methods are exactly the same.

+6
source

This is a personal style issue, and different people will give you different opinions; there is not a single correct answer. In terms of performance, it does not matter; the compiler optimizes them to be exactly the same (I just experimentally tested this).

Personally, I think the extra else prettier. However, this may be my functional programming background at work.

+2
source

In terms of the exact code snippets you provided:

  • The compiler optimizes two versions to the same bytecode, so there is no difference in performance

  • In terms of style, there is no generally accepted rule. Some teams prefer to leave else because it is redundant, others prefer to include it because it more reflects your intentions. Personally, I tend to include else , as it catches random errors.

eg:

 public Object foo(int i) { if(i > 0) { this.someMethod(); // !! Opps forgot to "return" here. } return this.anotherMethod(); // This method always returns 'anotherMethod' } 

( Edit : I missed the final return from my first version, I think this helps prove my point;))

If there was else ( else return this.anotherMethod() ), then the compiler will else return this.anotherMethod() error so that the first branch never returns a value. Therefore, I believe that placing else in place helps catch errors.

In a more general case (whether to include else or not), there will be a situation where the compiler cannot optimize it as easily as in your simple example. In this case, leaving else will give you less bytecode instructions, but whether it will be faster or not depend on the JVM, JIT and the processor you are running on.

These kinds of optimizations are rarely worth worrying about - always go with which part of the code you (and your teammates) think is more readable + supported.

0
source

The first is better because else is not needed. You never want to include an unnecessary part in a design, whether it be a Java program or a car or something else.

UPDATE:

Necessity does not mean utilitarian in common sense. If, for example, redundancy is designed to protect in depth or aesthetics, then this is not superfluous. Understanding the published question is simplified, below is an example where an unnecessary other complicates the design because an additional return statement is required.

 public static int add(int a, int b){ if(a > 0){ int c = a+b; return c; } else if( a<= 0){ int c = a-(-b); return c; } } 

bty: while the example reduces the argument to an absurdity, it illustrates the point of unnecessary parts.

-1
source

The first one is better because it is better even better if you can remove multiple exit points, i.e. just one return or as little as possible, and you can certainly do it here.

Also, please use codereview.stackexchange.com for recall type questions.

 public boolean test(boolean var){ boolean flag; if (var == true){ flag = true; } return flag; } 
-1
source

Source: https://habr.com/ru/post/1400126/


All Articles