4 ms·
I for one actually prefer to put a different type on the variable than the implementation. I.e. in the given case, I would actually use: `List<String> a = new
by mmatants 11y ago
I for one actually prefer to put a different type on the variable than the implementation. I.e. in the given case, I would actually use:
`List<String> a = new ArrayList<String>();`
I know that for some things it is convenient, but I appreciate the reminder to treat interface and implementation as distinct concepts.
- involans 11y agoFor local variables within the body of a method it doesn't really matter. As long as the method signature is expressed in interfaces, you should be able to use whatever implementations you want within the method body.
- usrusr 11y agoExcept when you add final, then there is little reason to have the less specific type. But i see where you come from, it feels a bit sloppy to not document the intention that the variable _could_ be any kind of list, even though it happens to be an ArrayList. Not "this may lead to nasty bugs"-sloppy, but a slight case of "this won't win any beauty contests"-sloppy.
- wtetzner 11y agoThis is for local variables, though. You can use var as the return type of a method. If your method bodies are so large that you need to be coding to interfaces within them (as opposed to just setting the return type of your method to the interface), the you've got other problems.