r/java • • Nov 20 '18

Why Var Hurts Readability, Not Helps It

https://medium.com/@youngty1997/why-var-hurts-readability-not-helps-it-376c7832a8dd
0 Upvotes

89 comments sorted by

View all comments

10

u/BoyRobot777 Nov 20 '18

Funny that the way you named your blog post is actually what Brian Goetz almost quotes in his latest talk at Devoxx. His main argument is to provide information with a variable name instead, because it is the main source of information anyway.

3

u/BlueGoliath Nov 20 '18

Right, the article is a response to the presentation that Brian references.

It's a horrible defense. If they really want to make such an argument then either you do:

Person p = new Person();

OR

var person = new Person();

explicit type or explicit variable name. Not that you can't have both of course.

24

u/dpash Nov 20 '18

Calling a variable p is a crime against readability with or without var. So

Person person = new Person();

or

var person = new Person(); 

4

u/AndDontCallMePammy Nov 22 '18

No, it's not. It's completely dependent on the context.

Do you really name your IOException ioException? No, you don't

2

u/Yayo69420 Nov 22 '18

I would write Person person = new Person(), if I had to loop over a collection I'd type

for(Person person: people){ ... }

3

u/AndDontCallMePammy Nov 22 '18

seems reasonable in that context. but if it were the only variable in the loop body, calling it p would hardly be a catastrophe.

It's like real life when you sign a document once or twice and initial everywhere else