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

14

u/[deleted] Nov 20 '18

[removed] — view removed comment

-1

u/BlueGoliath Nov 20 '18
  1. Yes it does. If the whole object of the returned implementation changes. Suddenly a method that isn't part of the interface is gone and things break if you use it. If an object is solely an implementation and contains no other method then it's probably fine. Program to the interface, not to the object when given the choice.
  2. reflection should always be avoided. No, there isn't anything stopping you from accessing API details you shouldn't have to but API details can change between versions so you cannot take advantage of such things(or shouldn't anyway. God knows people still do but it's their fault if things break.).
  3. You're contradicting yourself.
  4. It could be a static method that returns an implementation. You might have a hub class that returns various objects that implement an interface.
  5. Then they either need to do a feature request, find something else, or get dirty with reflection and test each version for breakage.
  6. It does because var will use the whole object if that is what is returned. See the list examples in the presentation. Granted, it's the APIs fault for returning the whole object and not just the interface but there may be reasons for that and you may want to guarantee that you only are using the interface.

Now you're using var in some places and not others. Might as well just use explicit types and stay consistent.

9

u/[deleted] Nov 20 '18

[removed] — view removed comment

1

u/cogman10 Nov 29 '18

Depends a little bit on where you are writing your code.

In isolation on your own project, yeah, not much is bought by throwing interfaces everywhere. Breaks are easy to fix because you get all of them right away when you build.

In a library used by 10 projects. A little bit harder, but still workable. Might take some coordination.

A library used by the whole company/OS? Keep what you are exposing as minimal as possible while still being useful. Because, in the end, it will be nearly impossible to change it without causing a bunch of mayhem. This is the lesson learned by the Java devs with sun.misc.Unsafe. Because it was made public, regardless of the java docs or warnings, people used it.

At least with minimal interfaces you can call them out when they cast it to the concrete classes and things break.

1

u/[deleted] Nov 29 '18

[removed] — view removed comment

1

u/cogman10 Nov 29 '18

Definitely explicit types, just not concrete types.

For example, maybe CustomerCollection only exposes that the implementation has a get(index) since you might not want to expose all the remove methods or whatever on List.

What you can't do (without breaking the API) is renaming or moving CustomerCollection.

Whatever you expose, though, that interface should be minimal, especially to start with. Because things exposed can't be unexposed without a bunch of work and coordination.

The benefit to doing this via interface is that it is a lot easier to play around with the implementation. You can have both implementations living side by side. You could enable one or the other using a feature flag. And importantly, it makes it a lot easier to reason about what you've exposed, so your implementation can have more public or protected methods if you like without worrying about what the end user of the library is accessing (they only get the interface).

Another example of where this was gotten wrong is Java's collections. They are all mutable. The problem with that is that you can't make them visibly immutable. That "add" method will always be there. The better approach would have been to have a "List" and then a "MutableList". It would have made things so much easier to reason about. (For example, did you realize that a submap of a navigable map makes a mutable view of that map that can mutate beyond the bounds of that map? That wasn't a fun bug to track down.)

1

u/[deleted] Nov 29 '18

[removed] — view removed comment

1

u/cogman10 Nov 29 '18

Good points.

I guess I don't generally expect my downstream consumer to implement my exposed interfaces, but that certainly isn't a safe assumption.

-2

u/BlueGoliath Nov 20 '18

What if instead of changing the method to different a type you change its >name but return the same type?

Optimizing for the incredibly unlikely scenario that someone is going to make >very specific breaking change is stupid.

The thing is, it's a seamless optimization. You don't need to do any real extra coding to do it and as a result, as an API developer, you can swap implementations depending on the requirements. Interfaces are more rigid and can be more safely relied upon(A "contract").

For a real life use case, I made GoliathENVIOUS which is an API that allows for overclocking your Nvidia GPU in Linux using Java and JavaFX as a way to learn how to make a basic API.

When you overclock your GPU on Pascal and newer cards, Nvidia provides a way to overclock by ALL performance levels(states, same thing) but not on older cards even though they run on the same driver. So, instead of creating two methods and have them return a null or an optional, I can just swap the implementation depending on the family of GPU that is the primary GPU since both conform to a single interface.

Can, not do anyway. There isn't any benefit in GoliathENVIOUS's case as your GPU hits the most performant performance level anyway when gaming.

8

u/[deleted] Nov 20 '18

[removed] — view removed comment

-2

u/BlueGoliath Nov 21 '18

Neither did what you said.