https://kotlinlang.org logo
#announcements
Title
# announcements
v

vach

02/08/2017, 10:34 AM
Hi all noticed this little thing and got curious why awesome kotlin team decided to do this consider this kotiln code
Copy code
fun enter(key: String) {

      if (disabled) {
         return
      }
      // lots of code
}
when i decompile kotlin bytecode to see what is the underlying java code i see this
Copy code
public final void enter(@NotNull String key) {
      Intrinsics.checkParameterIsNotNull(key, "key");
      if(!disabled) {
          // lots of code moved inside inverted if      
      }
}
not a big deal just curious why inverting the if statement? i cant think of some low level performance trick that may be the reason for this
e

elizarov

02/08/2017, 10:41 AM
vach: I think this is the question to the authors of decompiler
v

vach

02/08/2017, 10:42 AM
i shall post somwhere else?
e

elizarov

02/08/2017, 10:43 AM
both ways to represent this logic are equivalent from any performance point of view. Why do you care?
s

sigurdsa

02/08/2017, 10:44 AM
It might be the decompiler tries to reduce nesting to make the code easier to understand. In your case it looks a bit silly, but it would do this for larger methods as well. http://codebetter.com/patricksmacchia/2008/03/07/a-simple-trick-to-code-better-and-to-increase-testability/
v

vach

02/08/2017, 11:08 AM
Siguard compiler exactly does the oposite
it nests my code inside if
but thanks for the link thats good stuff
@elizarov i dont 🙂 Usually when i find stuff like this it turns out to be some sneaky jit jvm level trick… so i was wondering if thats the case...
My guess is it tries to remove multiple
returns
by restructuring ifs…
anyhow if there is nothing interesting here just ignore guys 🙂
o

orangy

02/08/2017, 11:34 AM
Kotlin compiler doesn’t generate Java, it generates bytecode. There is no such thing as
if
in bytecode, they are lower level constructs. So decompiler tries to reconstruct Java from bytecode it sees, and there are potentially a lot of ways to represent particular bytecode in equivalent Java syntax.
v

vach

02/09/2017, 2:13 AM
Ah right thanks Ilya
4 Views