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 declarations like this
new DoSomethingCmd(new SelectionContext(context, keys), infoStuff.getCurrentRole().getRole_id()).execute(getResultContainer());
will give the application a considerable performance boost. They are willing to sacrifice code readability for this. Are they right in claiming this? Is the above version significantly more performant than, say, this one?
final SelectionContext selectionContext = new SelectionContext(context, keys);
final String roleId = infoStuff.getCurrentRole().getRole_id();
final DeleteSomethingCmd deleteSomethingCmd = new DeleteSomethingCmd(selectionContext,roleId);
deleteSomethingCmd.execute(getResultContainer());
I realise that the first statement isn't all that difficult to grasp in and on itself, but the complexity adds up rather quickly when most of your code is structured like that.
Thank you for your input.
Correctness is the most important If your code is not correct at all, then it does not matter how readable or performant it is. There are some products where the whole system or part of it revolves around performance. In these cases, your product cannot be correct if it does not match the expected performance.
Readable source code facilitates the reading and understanding of the abstraction phases and as a result, facilitates the evolution of the codebase. Readable code saves future developers' time and effort.
Readability in software programming can be defined by the ease with which the software is read and understood. Readability of software can be somewhat objective. Programmers who are “journeymen” and move from one project to another throughout their career tend to have an easier time reading a variety of software code.
Software performance only matters for a very small fraction of all source code. But what matters is the absolute value of this code, not it is relative size. Software performance is likely to be irrelevant if you have few users and little data.
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).
Are they right in claiming this?
No, they are not. The cost of storing something in a local variable and then subsequently reading from it is next to 0, it's as simple as that -- this is definitely not something that you should spend your time optimizing, or even worrying about for that matter. Don't sacrifice readability by putting everything in a single, highly convoluted line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With