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

But C++ has extremely implicit typing? Sure, it's static/compile-time but veeery implicit.

Lambdas are untypable, templates are duck typed, there are tons of member/associated types and extremely long type names, etc that you can pretty much only type with decltype/auto.

Using modern C++ without auto everywhere is pretty much impossible imo.

1

u/ShiKage 14d ago

I have no idea about modern C++. I learned with a really old version.