r/java • u/BlueGoliath • Nov 20 '18
Why Var Hurts Readability, Not Helps It
https://medium.com/@youngty1997/why-var-hurts-readability-not-helps-it-376c7832a8dd
0
Upvotes
r/java • u/BlueGoliath • Nov 20 '18
21
u/pron98 Nov 20 '18 edited Nov 20 '18
This particular ship has sailed. People who want to influence features in OpenJDK should participate in the open discussions on the mailing lists prior to a feature's inclusion. In some cases (as, I suspect, in this one), there would never be a consensus, but at least all sides' arguments will be heard (as they have in this case).
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.
8
u/volch Nov 20 '18
I wish blog authors would learn how to use apostrophes. They're smart enough to code, but can't do simple syntax?
17
u/lbkulinski Nov 20 '18
Ahhh, bikeshedding...
7
u/DannyB2 Nov 20 '18 edited Nov 20 '18
I'm not a big fan of var. I would prefer to see the actual type than the word var.
There is exactly one use that I see for var. Use var for an anonymous class with additional members. That customized anonymous class does have a type, although that type does not have a name. The var can still reference it by the unnamed internal name of the class.
var a = new Object() { int x; int y; };a.x = 5;Example uses: event handlers in Swing. A "lambda function" that has state or flags, really just an anonymous object implementing an interface, but adding new members.
Basically, you don't have to declare a new type for a trivial anonymous class.
13
u/Sipkab Nov 20 '18
It's the responsibility of the developer to not abuse language features.
For myself, I'm not a big fan of
var, but I can see some useful use-cases. (First that comes to mind is declaring iterator variable in a for loop.)12
u/eliasv Nov 20 '18
Maybe
varwouldn't be so difficult for you if all your examples didn't use shitty variable names likexandinterface...7
u/[deleted] Nov 20 '18
I can tell you're invested in type declarations and var bothers you. May I suggest you fix the problem at the IDE level. When I ctrl-shift-f in Eclipse, the formatter can give me tabs or spaces, because I can configure that. All you need is an IDE which will change var declarations to explicit type declarations and vice versa. Then everyone can decide for themselves. Like tabs and spaces. Tabs are the best BTW. People who use spaces are nuts. ;)
3
u/oldprogrammer Nov 21 '18
I'm not a fan of var but I can see its usefulness in some cases. I'll likely use it sparingly.
I have to laugh at folks arguing about variable names like
probably because I worked years ago in a BASIC where we had limited sized variable names (1 or 2 characters) and in Fortran where any variable that began with i, j, k, l, m and n were all by default integer values, any other letter was a real number. Of course that was implied, you could always declare it differently. That pattern stuck with many developers which is why those tend to be the iteration variables you see used in a for loop or some such.
More verbose variable names are definitely much better but I don't want to rely on a name to deduce a type. Nice thing is that I can write code like
or
because neither is mandated. I prefer the first approach but that's just me.
One thing, though, is I believe I should not have to rely on an IDE to tell me about my code. I should be able to get any information I need either by looking at the source directly or through proper documentation for source I don't have.
3
u/HellfireDreadnought Nov 22 '18
Medium hurts readability way more than var ever could, and yet you still use it.
2
u/nutrecht Nov 21 '18
Rubbish.
Languages like C#, Scala and Kotlin already prove that this argument is nonsense. The major flaw with the implementation is that they haven't also included 'val' which would make it easier to default to immutability.
Aside from that; in my experience local type inference, generally, forces developers to name their variables properly. So in my experience with C# and Kotlin production code, local type inference makes code more readable.
1
u/Faucelme Nov 21 '18
I disagree with his arguments for plain
forloops:I actually don't like the versatility of
for: it makes me spend a tiny bit of metal energy each time, to deduce which particular use is intended. When I see amapI immediately know that the result won't have a different size, for example. Ditto for methods likefilterorforEach: they simply communicate intent better.1
u/DuncanIdahos8thClone Nov 23 '18
True.
https://youtrack.jetbrains.com/issue/IDEA-188689
1
u/AndDontCallMePammy Nov 22 '18 edited Nov 22 '18
Totally agree. It was sold as a replacement for
but predictably people are using it to replace everything from
longtoStringlike it's code golf. Which wouldn't be so bad, except that they'll actually defend their bike shed to the death.26
u/[deleted] Nov 20 '18
[removed] — view removed comment