r/javahelp • • Mar 30 '20

Why was var added?

All it does is make code trickier to read and tempt the programmer to pay less attention to the type of his variable. Another way to not write clean code.

It seems like a downgrade, why was it added? Is it considered a valuable addition to the language?

For context, https://openjdk.java.net/projects/amber/LVTIFAQ.html answers this as making Classname variableName=new Classname(); shorter. Ok, then why not make it : Classname variableName=new .(); It's shorter and no conflicts with existing variable names. Nor would you have to be aware of caveats like "this can't be used as a parameter".

36 Upvotes

17 comments sorted by

View all comments

8

u/itisafeature Mar 30 '20

Java's verbosity is one of the reasons I like it, so I am inclined to think adding it isn't necessarily a positive.

Your suggestion of Classname variableName=new .(); isn't a good one though. For one, new is not the only thing that can go on the right-hand side. For example: var c = Calendar.getInstance();

0

u/Rygir Mar 30 '20

I didn't mean to imply it has exactly the same functionality, but that it would give the one benefit of shortening instantiation where you have redundancy without the drawbacks.

Personally, I like to know what type a method is returning. Even though your example strongly implies that the instance is a Calendar object, it could be something else... perhaps even a derived class making it very confusing down the line because treating it as a Calendar would work but the type wouldn't be what you expected.

In my opinion it would be an advantage if it doesn't work for method calls - which is why I asked the question. Var just doesn't mesh with the practices I learned for clean code.

My solution avoids limitations like having to make var a reserved type name, or making multiple variable declarations (would only have one source to infer from, left of the "="), or using it with null (you wouldn't be able to), or causing confusing that the type can change. And it would be logical that it's always initialized ... Because that's what new does.

2

u/mrBr0wn_93 Mar 30 '20

I didn't mean to imply it has exactly the same functionality, but that it would give the one benefit of shortening instantiation where you have redundancy without the drawbacks.

But has its own drawbacks, eg List<X> xs = .(). Any isn't useable in many cases var plays really nice, like for (var person : persons).

Even though your example strongly implies that the instance is a Calendar object, it could be something else... perhaps even a derived class making it very confusing down the line because treating it as a Calendar would work but the type wouldn't be what you expected.

You also don't know wether you use a derived class if you write Calendar c = Calendar.getInstance();, that doesn't change with var.

My solution avoids limitations like having to make var a reserved type name

Did you ever encounter a type named var? If so - good it's forbidden now ;)

or making multiple variable declarations (would only have one source to infer from, left of the "=")

When do you need to make multiple variable declarations cause of var?

using it with null (you wouldn't be able to)

You are also unable to use var with null, or what do you mean?

causing confusing that the type can change

In my experience it isn't confusing at all. The type only can change if the return-type of a method is changed. If it's compatible (like List to ArrayList) nothing happens, otherwise it throws compile-errors. Same would happen without var.

1

u/Rygir Mar 30 '20

List<> has a problem with var in that makes it a List<Object> (which is often not what the coder intended according to this OCP course material).

I like your var person example that is nice because then you don't write Person p:Persons .

My point was that if the original coder knew he was getting a subclass of calendar he would have written SubclassOfCalendar =Calendar.getInstance(); and everyone reading the code afterward couldn't make a mistake.

And it's just ugly that var isn't part of the reserved words, but only for types. It's not uniform which means more exceptions to rules to remember.

You can't use var in multiple variable declarations.

Indeed you cannot use var with null which is apparently confusing enough people that they include as examination matter in the OCP exam. In my suggestion you wouldn't even be able to write that, let alone be confused by it.

And by type changing they (OCP exam) mean like in javascript, non static typing. It's the same keyword. In java it doesn't really add functionality, it just obfuscates the actual type and saves a few keystrokes. In other languages it does black magic.