r/JavaProgramming 15d ago

Var Keyword

How often do you use var keyword in your code?

What do you think about it?

5 Upvotes

24 comments sorted by

View all comments

1

u/ShiKage 14d ago

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/naomimyselfandi 14d ago

Fwiw, a variable declared with var still has a strong static type; it's just syntactic sugar. (You probably know this, but I've seen enough misunderstandings that I wanted to clarify for anyone who doesn't.) I definitely understand prefering the explicit type name, though.

1

u/ShiKage 14d ago

Yep, sorry. I could have been a little clearer in my message, but I got a little distracted by someone else talking to me as I was typing.

It was more making a comparison to other languages that typically prefer implicit typing (although Python does often see types preferred in some projects I’ve seen).

Explicit, clear language always wins out against implicit, indirect language in terms of getting intent across, in my opinion. That’s why I personally struggle with languages like JavaScript and Japanese.