java - Memory/Performance differences of declaring variable for return result of method call versus inline method call -
are there performance or memory differences between 2 snippets below? tried profile them using visualvm (is right tool job?) didn't notice difference, due code not doing anything.
does compiler optimize both snippets down same bytecode? 1 preferable on other style reasons?
boolean valid = loadconfig(); if (valid) { // ok } else { // problem } versus
if (loadconfig()) { // ok } else { // problem }
the real answer here: doesn't matter javap tell how corresponding bytecode looks like!
if piece of code executed "once"; difference between 2 options in range of nanoseconds (if @ all).
if piece of code executed "zillions of times" (often enough "matter"); jit kick in. , jit optimize bytecode machine code; dependent on lot of information gathered jit @ runtime.
long story short: spending time on detail subtle doesn't matter in practical reality.
what matters in practical reality: quality of source code. in sense: pick option "reads" best; given context.
given comment: think in end, (almost) pure style question. using first way might easier trace information (assuming variable isn't boolean, more complex). in sense: there no "inherently" better version. of course: option 2 comes 1 line less; uses 1 variable less; , typically: when 1 option readable another; , 1 of 2 shorter ... prefer shorter version.
Comments
Post a Comment