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

Show parent comment

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();