I have a variable that I’m using many times in a class and as speed is important I would like to use the fastest method.
Is there a difference in speed between using:
modifyValue(User.getSession().getSessionValue());
and setting the info to a local value such as
private String sessionValue;
sessionValue = User.getSession().getSessionValue();
modifyValue(sessionValue);
when using the variable in several different places? Which is better style, or clearer to understand? If the above example is currently being used in existing code is it worth the time to refactor?
I don’t believe there’s a speed difference. This is the kind of micro-optimization that’s likely to lead you astray.
There might be a a thread safety issue with a writable private member variable if it’s not synchronized properly.
I would prefer the first one because it avoids unnecessary writable state.