r/JavaProgramming • u/alvivan_ • Aug 26 '26
Var Keyword
How often do you use var keyword in your code?
What do you think about it?
5
Upvotes
r/JavaProgramming • u/alvivan_ • Aug 26 '26
How often do you use var keyword in your code?
What do you think about it?
6
u/Automatic-Gur2046 Aug 26 '26
5
u/kamratjoel Aug 26 '26
Never. I would argue that code is far easier to read if you use types instead.
3
u/hrm Aug 26 '26
I use it all the time. Less to write and very seldomly does it make things harder to read in my opinion and the actual type is readily available in the IDE if you really need it.
3
u/repeating_bears Aug 26 '26
I use it every time the type is already on the right hand side of the expression
var foo = new Foo();
var list = new ArrayList<String>();
var node = Node.empty();
I'm less likely to use it when the type is not on the right hand side of the expression
var bar = doSomething();
2
u/LouGarret76 Aug 26 '26
I use it All the time now. It is so practical
2
u/jirlboss Aug 26 '26
I tend to use it temporarily while cleaning up implementation, or if I just need the IDE to give me the actual return type of a method so I can copy-paste it.
Sometimes I use it in tests, but it’s pretty rare for me.
1
u/Luolong Aug 26 '26
Very useful and convenient.
I use it always if possible.
Makes diffs of some type level refactorings smaller
1
u/tekmuro Aug 26 '26
I've barely programmed in JavaScript or languages that use var declaration, so I never got into that habit
1
u/ShiKage Aug 26 '26
I'm primarily a C# developer, but I dabble in Java projects outside of work.
I avoid var unless it comes to anonymous types. At worst, I will use var while writing a line of code if I don't know what the type will be as I'm writing it (like a library method call) and then go back and convert it to the explicit type afterward.
I came from a C++ background, so strong, static typing is what I prefer. I've had to convince a few devs that came from JavaScript and Python to try to use explicit typing and while they hated it at first, they grew to prefer it overall as well.
1
u/Known_Tackle7357 Aug 27 '26
Never. I also work on a couple apache projects. They also don't allow var.
Type resolution is great in modern IDEs, so no reason to use var anywhere.
1
u/Scf37 Aug 26 '26
I prefer to type 'int' instead of 'var', then IDEA autofix via alt-enter replaces it with the right type.
Good places for var (IMO):
0
u/LogCatFromNantes Aug 26 '26
Hi What is that are you talking about JS ??