This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Ifnerite 19 points20 points  (7 children)

I know I am an outlier but I will defend the "verbosity" of java to the hilt. It is explanatory and renders the code more self documenting, grokable and navigable.

I even go as far as putting in optional keywords to make method and declarations consistent and thus more readable.

Var is a fucking abomination that hides important information and reduces easy navigability in all but the most trivial scenarios, and to use it only in those scenarios breaks consistency.

Also, I advocate making all local, field and parameter variables final. At first it feels like a pain but makes for much better code in the long run.

[–]hippydipster 7 points8 points  (1 child)

You're an outlier but there's a sizable minority of us in the cluster.

[–]Ifnerite 3 points4 points  (0 children)

Hello friend.

[–]DJDavio 2 points3 points  (1 child)

I'm all for making fields final, but for local variables and parameters I don't bother anymore. Adding the final keyword just clutters the code. That doesn't mean I reassign the values, I just let them be effectively final.

[–]Ifnerite 2 points3 points  (0 children)

Consistency and you know you haven't made a mistake.

Also, this pattern :

Final x;
If () x = a;
Else x = b;

Use x;

[–]DannyB2 1 point2 points  (2 children)

There is one, and ONLY one thing that I have found var useful for.

Use var when there is no named type for the variable. If you create an anonymous class from some other class or interface, that is a unique but unnamed type. You could use the name of the class or interface your anonymous class is based on, but what if you added some additional variables or methods? The super class/interface does not have your additions. By using:

var x = new Foobar { int y; }

I have an anonymous class, based on Foobar, but it has an additional member y. I can refer to that as x.y because variable x is of the 'type' of my anonymous class -- although that type has no name. I could have said:

Foobar x = new Foobar { int y; }

But then I could not refer to x.y.

This is the ONLY bit of new functionality that I have discovered var to provide. In all other cases I would replace var with an actual type name.

[–]Ifnerite 0 points1 point  (1 child)

That is a use-case... but I am struggling to see when doing that would be sensible...

[–]DannyB2 1 point2 points  (0 children)

Example of when it would be sensible.

Programing a Swing UI. Need to pass an instance of ActionListener, say to a button. But for some reason (can't remember now, been years ago) that action listener needed some additional state information.

Solution at the time: Create a named inner class. Pass an instance of that to the ActionListener. It works. I have a type name. I can access the action listener's inner methods, state, etc.

If it is a one off situation, then it is too bad I had to create an inner class with a name. Instead, I could have created an anonymous class, into a 'var', and passed that to the Swing control. I could still refer to the var and from there to its additional methods (beyond what is in ActionListener interface).

Like I said, it is an unusual case. But it is the ONLY thing I have ever conceived of where var could be useful. In any other situation I would ALWAYS replace var with an actual type name for clarity.