Admittedly Java has had the var
keyword for a few years now, since Java 10 (and C# obviously has had it for a lot longer), and while I was involved in a few “for or against” discussions at the time, I realize I never officially posted my position on the matter. So here goes.
I recommend not using the var keyword in Java
In my case, it is not simply personal taste or a matter of opinion – I actually believe it makes you less productive. I base this belief upon two facts.
1. Developers spend much more time reading code than writing code. For example, in Clean Code: A Handbook of Agile Software Craftsmanship Robert C. Martin claims “the ratio of time spent reading versus writing is well over 10 to 1”.
That is, developers spends more than 90% of their coding time reading existing code.
So while the var
keyword may save you a little bit of time while writing code (I would say very little with a good IDE), if you only spend 10% of your time actually writing code – isn’t it more interresting how the var
keyword affects your reading…?
2. Eye movement analysis of people reading code show that we are repeatedly revisiting the initial variable declarations (reference at the end). It seems that, even in cases with only a few local variables, the mind needs a constant refresher of what these variables mean, and – at least I’m assuming – their type. This happens so frequently that researches even gave the behaviour a name – “retrace declaration”.
Now, let’s assume a method with this piece of code.
var ratio = foo.calculateRatio();
When reading this line, what type is the variable? int
? double
? String
??? A Map
keyed by class X with the ratio double
as value? Admittedly, upon first read it may not matter (which is kind of the idea with var
, right?). But when your mind and eyes are repeatedly doing declaration retraces, do you think the process will be faster or slower compared to if explicit typing was used, such as
int ratio = foo.calculateRatio();
or
Map<Customer, double> ratio = foo.calculateRatio();
?
I know what I believe. And I believe the case is the same when initializing with non-return values, such as
var foo = 1; var bar = new HashMap<String, Integer>();
vs
int foo = 1; Map<String, Integer> bar = new HashMap<>();
In the var
case you’d need to read more or less the whole line (well, you could skip var
itself) to realize of what type the variable is. In the explicitly typed example however, I’m assuming it is enough for the brain to pick up the following, beforing allowing it to re-realize the type and hopefully resume analyzing the use of the variable:
int foo Map<String, Integer> bar
Admittedly I have not seen any studies on this particular matter, but assuming I’m right, it would mean explicit typing lets you read less number of characters (or rather “words” since the brain normally doesn’t process character by character) than you would have needed to read with var
, and thus using var
would force you to spend more time reading code than explicit types.
And since reading makes up 90% percent of your coding, in total you would be less productive, even if you saved a second here or there of your typing.
If your experience is contrary to the above or, better yet, if you know of detailed studies explicitly focusing on eye movement / productivity effects of var
, please let me know in the comments.
Reference for the eye movent study: Uwano, H., Nakamura, M., Monden, A. and Matsutomo, K. Proceedings of the 2006 Symposium on Eye Tracking Research & Applications as referenced with charts and all by Jason Cohen in Best Kept Secrets of Peer Code Review.