Recently we had a discussion at work about the impact of local variables on the performance vs readability of Java code. Some of my colleagues are of the oppinion that declarati
The only thing that the "optimized" version makes is that you have a few variables less in the stack, slightly increasing your memory consumption. Performance should be measured carefully (google how to benchmark an issue), but I seriously doubt that it has any noticeable effect.
Also, spending time improving performance in a piece of code that is not used often is just a waste of developer time, which is expensive.
In this case, readability should win the day.
EDIT: Anyway, if you use proper indentation, I do not thingk the two versions are too different in readability terms:
new DoSomethingCmd(
new SelectionContext(context, keys),
infoStuff.getCurrentRole().getRole_id()
).execute(getResultContainer());
The advantage of this text is that you do not have defined variables (selectionContext
, roleId
)that are no longer needed (so when you read the method again they do not mix with more "persistent" variables). Anyway, that is open to interpretation; the bottom line is that you should not worry with optimization unless you have a motive to do so.
Apart from that, there are some guidelines to Java programming that give you really useful tricks that really help you (v.g. using StringBuilder
to concatenate strings).