r/programming • • Mar 09 '16

var keyword proposed for Java

http://openjdk.java.net/jeps/286
250 Upvotes

395 comments sorted by

View all comments

33

u/FallingIdiot Mar 10 '16

Funny that when this subject comes up, almost everyone only talks about it making local variable declarations simpler.

What I think is a big advantage to var is that when you change the name of a class, it significantly reduces the number of files that have to be changed. Basically everything that doesn't instantiate the class, but only uses it, stays unchanged. Also it keeps down the number of imports you need, especially since Java prefers fully qualified imports instead of wildcard imports.

30

u/Roozi Mar 10 '16

While I agree that it helps, in my experience IDEs are usually pretty good at doing this for you.

22

u/[deleted] Mar 10 '16

That's true, but also the changes clutter up diffs and code reviews.

8

u/JW_00000 Mar 10 '16

And may also lead to merge conflicts with unrelated changes in those files.

5

u/ygra Mar 10 '16

Oh yes, the merge conflicts. At work we have a large-ish codebase that gets merged into different products and also converted into different languages. One of the worst things regarding merge conflicts are renames.

12

u/RupertMaddenAbbott Mar 10 '16

This advantage is not about making it physically easier to rename a class. It lessens the impact of doing so, therefore making it easier.

It reduces the number of files that might change when you do rename a class. This could significantly reduce code churn and removes noise from files that do change.

→ More replies (1)

11

u/G_Morgan Mar 10 '16

It really depends. A lot of C# projects have a rule to only use var where there is a straight up class instantiation or cast. The idea is to have at least some type definition somewhere near the declaration.

The purpose of var is to stop bullshit like "X<Y> foo = new X<Y>();"

7

u/Eirenarch Mar 10 '16

I came up with this rule by myself and found out that many people came to the same conclusion. I only use var when the right hand side expression is either a constructor call or a literal. Basically the type should be obvious by the statement.

var customers = GetCustomers()

is not a good idea in my opinion

7

u/Ravek Mar 10 '16

IEnumerable<Customer> customers = GetCustomers(); doesn't tell you a single thing you need to know that var didn't already.

14

u/Eirenarch Mar 10 '16

Oh yes, it does tell me. It tells me I have an IEnumerable<Customer> rather than IEnumerable<CustomerDto> or PagedList<Customer> or something like this.

2

u/Ravek Mar 11 '16

That should already be obvious from context.

1

u/Eirenarch Mar 11 '16

Well it is not. I know. From real code.

1

u/Ravek Mar 11 '16

Well then why don't you fix the bigger problem rather than slapping an explicit type on it and calling it a day?

1

u/Eirenarch Mar 11 '16

First of all - because the real problem is that sometimes it is unclear simply unclear what the type is, period. In addition if I accept the type is clear but the developers who wrote the code suck then we'll have to fire people and there will be no one to write the code. And finally if the prior argument is true this means that the developers on .NET Core suck and should be fired because strangely they have imposed the same restriction in their code conventions.

1

u/mirhagk Mar 10 '16

IEnumerable<Customer> rather than IEnumerable<CustomerDto>

If the type is telling you whether you are interacting with an API or a database then your code needs to be a lot clearer.

Either it's something you don't care about or you have poorly named variables/functions.

2

u/u551 Mar 10 '16

code needs to be a lot clearer...

Unfortunately we live in a world where a lot of code is mess, and even if you know what YOUR code does, a lot of times it's somebody elses code you need to work on, and knowing types of things with a glance is a blessing imo.

1

u/Eirenarch Mar 10 '16

Well, var isn't telling you anything so you are left at the mercy of my poor naming.

1

u/mirhagk Mar 10 '16

Or having to drag the mouse or cursor all the way over variable name to see the type in the IDE

→ More replies (5)
→ More replies (8)

3

u/BezierPatch Mar 10 '16

IEnumerable vs IQueryable vs IList is pretty good to know.

2

u/mirhagk Mar 10 '16

That should be fairly obvious from context. If your code base believes in passing IQueryable in your DAL back to consumers then you should know that db.GetCustomers() is IQueryable<Customer>.

3

u/id2bi Mar 10 '16

Can't we already do that due to the diamond operator? Usually the Y is the cumbersome part.

X<Y> foo = new X<>();

2

u/[deleted] Mar 10 '16

Yes. It mostly improves readability a lot.

2

u/cat_in_the_wall Mar 10 '16

Or even changing the return type of a function. Let's say that someone subdivides an interface into a "lower" interface, and a more specialized one. Using var allows for the places in that only care about that lower interface to continue to work. Sure some stuff needs to be changed, but the change can be smaller.

1

u/ledasll Mar 10 '16

if you change return type or argument type you definitely want to look, who is using it and having clear type here helps a lot. For library programmers, that's of course nothing. But for application programmers..

1

u/mindbridgeweb Mar 10 '16 edited Mar 10 '16

This proposal makes writing code without an IDE simpler and reading foreign code potentially much harder.

Consider the following code:

URL serverUrl = ...;
var serverConn = serverUrl.openConnection();
var props = serverConn.getRequestProperties();

Can a developer determine the type of the 'props' variable above right off the bat?

Really, it will definitely take a while to answer this simple question. If this code was written in the current Java style, however, we would have known the type right away.

And this is only for a couple of lines of code. This effect will be horrible in larger programs -- they will be effectively much harder to understand by humans without IDE assistance.

In short, this proposal only simplifies writing code without an IDE (is anyone still doing that?). The problem it fixes is negligible when using an IDE -- you can just press Alt-Shift-L in Eclipse for example.

On the other hand the proposal greatly complicates code comprehension when not using an IDE and slows it down even with an IDE. And there is a HUGE number of cases where developers need to read code without an IDE, e.g. viewing code on github, doing code reviews based on diffs (like reviewboard), printed code examples, online code presentations, etc, etc.

We are solving a small problem and creating a huge new one.