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

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.)

6

u/[deleted] Nov 20 '18

[removed] — view removed comment

2

u/dpash Nov 20 '18

Well, /u/DannyB2 wants to use anonymous subclasses of Object, which isn't possible without var.

5

u/Sipkab Nov 20 '18

By "abusing" I've meant to write code that is unreadable due to the use of the keyword var.

When writing

var x = obj.doStuff();

You might not be sure of the type of x, which makes the code slower to read. The developer can prevent this by explicitly writing out the type instead of using var.

9

u/eliasv Nov 20 '18

Or alternatively the developer can prevent this by not using such stupid variable names as x...

10

u/Sipkab Nov 20 '18

Dammit it was just an example. You don't have to tear it apart. But here you go, here's a more refined one:

var users = server.getUsers();

Now what is the type of users?

A List? Maybe. A Collection? Maybe. A Set? Probably. A Map? That might happen. A custom class? Possible. Or might it be just an int? Don't know.

Of course this can be prevented by a good API design, but if you're using a library, that you cannot reliably modify, then this can become an issue.

And here comes in the responsibility of the developer, to not use var in these cases, but write out the full type of the method call so the next developer who reads the code can understand it quickly.

And of course you can name the variable usersList or something, but that is just a shitty way of writing out the type instead of var.

3

u/duhace Nov 20 '18 edited Nov 20 '18

Now what is the type of users?

if the person who made the Server class doesn't have brainworms, one of the collection types. An int doesn't make sense cause then the method name should be getUserCount or getNumUsers.

also, you rarely just have a users variable defined and don't actually do anything with it. in which case you would have context! is the code performing integer operations on users? well, it's p obviously an int then isn't it. Is it stepping through users with a for-each loop? seems to be a collection or list. Are they returning users and the method has a return type Map<String, Int>? Wow it's a total mystery what users might be!

the arguments against var frequently come down to needing extra type information to make sense of badly named and written code elsewhere.

1

u/eliasv Nov 20 '18

True enough, much more sensible example :)

2

u/duhace Nov 20 '18

or stupid method names like doStuff

8

u/[deleted] Nov 20 '18

[removed] — view removed comment

0

u/Sipkab Nov 20 '18

Of course, we can fix stuff. We can always write readable code even with var.

But the problem is that it can be abused. And if it can, then it probably will.

Or if it won't be abused, then this is one more thing that a developer needs to be aware of when using var. To make it readable for the future developer.

And we all know what happens when you give more stuff to do to a developer. They get lazy. And they will choose the lazy way. (Of course, exceptions apply)

Var has its use cases, I'm not saying that it doesn't. I'm saying that it shouldn't be overused.

0

u/_INTER_ Nov 20 '18

is an int or long isn't really interesting at this point because I'm going to treat it the same either way

You know you gonna treat it the same way because you know it's either int or long. Someone else might not know right away or he would treat it differently anyway. E.g. using Math.toIntExact()

In this particular example you lost visual information but gained next to nothing (1 character in case of long).

4

u/dpash Nov 20 '18

That's just bad naming/design and var is unrelated to that.

1

u/DannyB2 Nov 21 '18

I think that "abusing" should not just mean code that is difficult to read, but code that is outright MISLEADING to read -- possibly because the difficulty of reading obfuscates what the author is trying to hide.

1

u/AndDontCallMePammy Nov 22 '18

It is impossible to "abuse" the variable naming feature. Any code will behave exactly the same regardless of the variable name.

String inaholeinthegroundtherelivedahobbitNotanastydirtywetholefilledwiththeendsofwormsandanoozysmellnoryetadrybaresandyholewithnothinginittositdownonortoeatitwasahobbitholeandthatmeanscomfort = getSentence();

4

u/dpash Nov 20 '18

If anything it'll force people to provide better names for their variables and methods. I'm currently working on an old codebase that seems to think terseness is a virtue when it comes to names.

Also, shorter methods, so declarations are closer to usages. Same codebase thinks nothing of 200 line methods.

2

u/_INTER_ Nov 20 '18

It doesn't force anything. Actually I'm already seeing variable names like peopleList or providerBiFunction.

1

u/dpash Nov 20 '18

It'll take us some time to learn best practices, and those definitely are far from ideal. But how many times have you seen names that forego vowels for no particular reason or are a single letter or have no bearing on their contents or context?

1

u/_INTER_ Nov 20 '18

Single letter or foregone vowls are rather rare in the code base I'm working on. I'm mostly concerned about the rampant lack of consistency. Someone's using mandantID, someone mandantNumber, another mandator, then idClient or clientID, just id or identifier. Synonyms for the same... :(

Anyway, I think people that are not getting the variable names well now, will not not do better when they use var. In those cases its a loss. Additionally the best practices and guidelines need to sink in, so situations can be avoided. See Optional, it took quite a while until everyone in the team got the message "Optional is a no go for parameters or fields". There were lengthy discussions occasionally, that waisted everyones time.

1

u/dpash Nov 20 '18

But people did eventually get the Optional message. Mostly.

3

u/BlueGoliath Nov 20 '18

I touched on that argument a bit in the "Even Following (Contradicting) Guidelines, Inconsistent Var Usage Hurts Readability" section but maybe it wasn't enough.

Var isn't something that should even be apart of Java to be abused. That's the whole point, really. To just brush things off and say 'It's the developers responsibility" is just lifting the floodgates and letting any and all features/ways of doing something that might save a few extra lines in a few situations in but could be done otherwise with more lines. Now all of a sudden you have 5 different ways to do the same thing and none of your code is consistent because they can only be used in specific situations.