27

Given that I can't use var for non-denotable types (Nulls, Anonymous classes, Some Single Method Class and most importantly Intersecting Types), am I better off not using it for better readability and consistency (given that it's only for local types)?

I kind of feel that it would be abused:

var a = someObj.getSomeValue().getSomethingElse().returnsSomething();
informatik01
  • 16,038
  • 10
  • 74
  • 104
Antho Christen
  • 1,369
  • 1
  • 10
  • 21
  • I would give the same advice as for `auto` in c++, only use this when you're sure there won't be conflicts (and abuse it for long types, we know our map entries). If you know the type (and it's not very long), you should use it. – Vivick Apr 26 '18 at 10:22
  • 4
    You _can_ use it for non-denotable types. – Jorn Vernee Apr 26 '18 at 10:25
  • 7
    FWIW I don't like it either, so my answer would be **you shouldn't use it**. You should always know the type as far as I'm concerned, and not writing it is arguably bad for readability. That and there's *many* more useful things that could be added to Java on a language level. That said, I'm an old fuddy duddy when it comes to these things, and there will clearly be others that disagree, so this really is just opinion based without a clear answer, and not on topic here. – Michael Berry Apr 26 '18 at 10:27
  • 9
    *Style Guidelines for Local Variable Type Inference:* http://openjdk.java.net/projects/amber/LVTIstyle.html – Stuart Marks Apr 26 '18 at 15:14
  • 11
    There are plenty of circumstances where type inference makes code _more_ readable. Of course, if you write bad code (poorly chosen variable names, super-long methods, etc), then leaving out type information might make your bad code even worse. So, write good code, and use it where it helps. – Brian Goetz Apr 26 '18 at 15:19
  • Are you saying that if you _could_ use `var` for all the types you wanted, that you would then write the given example code with no qualms about it? I'd suggest that unless it were blindingly obvious in context, the example code is likely to be a bad choice with any kind of type inference in any language, regardless of specific mechanics. – Shorn Apr 26 '18 at 22:27
  • 2
    IMO it adds ambiguity to code. So NO. For instance, `var var = 10`, the var here is inferred as a long. – Gayan Weerakutti Mar 29 '21 at 17:59

1 Answers1

27

Though this is probably primarily opinion-based, I'd say: No, it is not evil.

The var keyword is only allowed when the type of the variable is already clear at compile-time. Also, it will generate the same bytecode, so there is nothing to fear.

In some cases like null, var is not allowed. This doesn't make it evil, but rather good. If this was not the case, Java would not be statically typed anymore. (Note, however, it is allowed in most of the cases you've listed.)

Also I don't see any problem with your example. Since your .returnsSomething() would rather be something like .getPerson() in the real world, it would be clear to the reader that var is a Person.

Brian Goetz
  • 90,105
  • 23
  • 150
  • 161
Impulse The Fox
  • 2,638
  • 2
  • 27
  • 52
  • `null` is the *only* case among the OP’s list which doesn’t work. And even for that case, I don’t see how supporting it, e.g. by always inferring `Object` for `null`, would imply that “Java would not be statically typed anymore”. – Holger Apr 27 '18 at 08:32
  • 21
    Regarding your last paragraph, it might be valid to assume that `getPerson()` returns a `Person`, but it doesn’t matter. Note how the OP is willing to write an expression like `someObj.getSomeValue().getSomethingElse().returnsSomething()` without knowing, what type `getSomeValue()` or `getSomethingElse()` are returning. That’s not different to writing `var x = someObj.getSomeValue(); var y = x.getSomethingElse(); y.returnsSomething()`… – Holger Apr 27 '18 at 08:33
  • getPerson() . That could be the DTO or the JPA entity ? – Daniel Jipa Aug 07 '23 at 07:29
  • Even in the Java code itself you have poorly chosen names. `var buffer = response.bufferFactory().wrap(message.getBytes(StandardCharsets.UTF_8));` – Daniel Jipa Aug 07 '23 at 07:35