DEV Community

loading...

Discussion on: What's new and interesting in Java?

Collapse
jckuhl profile image
Jonathan Kuhl

My favorite "new" thing in Java is something as simple as var.

I hate duck typing. I hate HTTPConnectionManager httpConnectionManager = new HTTPConnectionManager().

var trims some of that away.

var httpConnectionManager = new HTTPConnectionManager() is still redundant but less so.

Collapse
rossdrew profile image
Ross • Edited

The problem arrises when devs use it too much and end up with nonsense like

var n = getValue();  //Integer, Map, int?

whereas old Java it would be clearer what was happening

Integer n = getValue();
//or
Map<Integer> n = getValue();

Conciseness will always be abused to create unreadable code.

Collapse
jckuhl profile image
Jonathan Kuhl

Yeah, it's my philosophy to not use var if it's not already obvious to the reader what the type is, even if it is obvious to the compiler.

For example I'd never do:

var thing = Otherthing.build(somethingElse);

Because it's not clear to the reader what the other side is returning

But I would do

var thing = new Thing();

because the type information just gets obnoxiously redundant and it's obvious to the reader.

I know people will abuse it, but I'd prefer to not waste time typing the obvious out

Thread Thread
rossdrew profile image
Ross

I'd love to use these features but I prefer they weren't there because most programmers will overuse them and lead to a headache for me.

I find the choice in production code tends to be dealing with forced verbosity or dealing with the absolute crazy stuff people will do when allowed to be more concise. I know which one wastes more time. It's sad but it's true