Saturday 15 March 2014

Java method vrs reference performance -



Java method vrs reference performance -

i prefer phone call method few times possible due perception have calling method has performance cost greater performance cost of calling 1 time , keeping reference. problem think got viewpoint c++ in college , things have changed lot since then.

so cheaper?

something.getordosomething(); something.getordosomething();

or

thing thing1 = something.getordosomething(); //use thing1 on.

now i'm aware method might contain lets assume don't. simple getters. there jvm performance optimizations should aware of?

i want clear because i've been seeing lot in legacy code.

since you're asking performance: it. doesn't. matter.

jit in jvm optimize bytecode @ runtime , execution of options faster think. don't worry on kind of micro optimizations unless it's proven bottleneck in application , demonstrated usage of tool profiler.

from comment:

if developers used coding way might not create repetitive calls method more getting?

here concern not performance. here reply be: it's matter of usefulness part of code beingness used, or code readability:

if few calls on getter through piece of code , it's little purposes, utilize getter. if need access field lot of times within single piece of code , need debug through changes done reference, certainly store in variable first, utilize it.

another factor take business relationship law of demeter:

each unit should have limited knowledge other units: units "closely" related current unit. each unit should talk friends; don't talk strangers. only talk immediate friends.

this is, example, having piece of code this:

public void somemethod(foo foo) { logger.info(foo.getbar().getfirstname()); logger.info(foo.getbar().getlastname()); logger.info(foo.getbar().getbirthday()); logger.info(foo.getbar().getbaz().getsomefield()); //and more other operations not straight involved logger.info... }

it should rewritten to:

public void somemethod(foo foo) { bar bar = foo.getbar(); logger.info(bar.getfirstname()); logger.info(bar.getlastname()); logger.info(bar.getbirthday()); baz baz = bar.getbaz(); logger.info(baz.getsomefield()); //and on... }

java methods reference

No comments:

Post a Comment