Edit: Okay, I got a lot of interesting comments here when I just made a joke. Still, it is fun to read it all and to see just bow dedicated the Java guys are to their language
I'm happy that in our company we moved (still moving actually) to Java 11, newer projects at least. My colleagues are still afraid of using var though, so nothing changed really.
This was not my experience when my company migrated java 8 -> 11. We saw a number of our services increase RSS size by around 1gb. There was oddly no accounting for the increase in memory size in the heap dumps or native memory tracking, we eventually just accepted that some services would use more memory.
Do you mean that there's more heap consumed by data or that the Java process takes up more RAM from the OS? If it's the former, it is indeed strange and does require investigation (e.g. the footprint of most strings has been nearly halved in JDK 9, and this is the general trend of overall footprint we see). If the latter, it's probably a simple matter of different defaults. Remember that a Java process takes as much RAM as you tell it; if you don't, it will take up some heuristically-chosen default value that can change significantly from one release to another (I think that the default for G1, which is the default GC since 9, is to now take up 1/4 of total RAM unless you tell it otherwise).
Already know the type, or don't care about the specific type. getObjects() obviously returns a container of Objects, you probably don't care what kind of container.
To an extent, assuming names are sensible. Personally I'd use var everywhere if it was just myself, but when working on a team I need to assure everybody can figure it out quickly.
"I burned my mouse years ago on a stake and exclusively use heavily modified VI running on a Raspberry clone I built with silicone dug up in my backyard"
You can't copy/paste text in a mouse over, a mouse over hides other parts of your screen, it takes more time to get your mouse in position than it would take to read it, I could go on.
The problem with this is code is difficult to read/skim through. When you're working with someone else's code it's very important to be able to skim through code quickly to get to the part that you need. Honestly skimming is half of the work in maintenance projects and bad use of var can make it very difficult.
If the variable will get written to (receive a reference to a different object) later in the code, its type may need to be a supertype of the function's return type. For example, if a variable will sometimes need to hold references to different kinds of List (Java)/IList (.NET) implementations, but is initialized to hold a reference to e.g. an ArrayList(Java)/List(.NET), declaring the variable as the class type rather than the interface type may cause problems later on.
They are idiots if that's their reasoning, because type inference is not dynamic typing by any means.
With dynamic typing the type is determined at runtime and can change during execution. In a statically typed language with type inference the type is still determined at compile time and can't change during execution, all the guarantees from the type system are still there. The only thing that changes is how readable the code is.
There were some fairly fundamental compat changes introduced in 9. Once you're on Java 9 it's more guaranteed that a jump will work. That being said, for 8 → 9, you're likely only going to encounter problems on a large project with a sprawling dependency tree.
Same justification as auto in C++. It makes your code more resilient to change and also helps reduce needless redundancy. getPlayerObjects is pretty self-explanatory, there is no reason to explicitly assign a call result to a List<PlayerObject> or a PlayerObject[]. You know it's a container of PlayerObjects, specifying the result variable type just adds redundant noise. var also resolves to the exact return type rather than a more abstract type that people would often use otherwise.
I personally use var quite often, but with few rules.
Use, when variable name is good enough to tell the full story - which should always be the case, but we arent perfect.
Don't use when ą type can't be determined by scanning the block it is in, which practically means that i avoid using it to assign the result of a method, unless it is obvious what the return type is.
Don't use when dealing with generics, eg Map, Collection, etc.
Don't use when dealing with generics, eg Map, Collection, etc
I actually feel that this is one of the better places to use var. I find that people tend to shy away from more complex types to fully express constraints. With var you can propagate more complex types without having to write it over and over.
215
u/Ravoos Sep 15 '20 edited Sep 15 '20
Java devs: Cool.....I'm still using Java 8 tough.
Edit: Okay, I got a lot of interesting comments here when I just made a joke. Still, it is fun to read it all and to see just bow dedicated the Java guys are to their language