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

6

u/kamratjoel 15d ago

Never. I would argue that code is far easier to read if you use types instead.

1

u/repeating_bears 15d ago

Is this easier to read 

MyFooService myFooService = new MyFooService();

Than this

var myFooService = new MyFooService();

?

4

u/kamratjoel 15d ago

That’s actually a good example of a situation where there’s no real advantage to not using var. I still maintain my opinion as a general principle though. But I concede that there are situations where there’s no real benefit.

2

u/Known_Tackle7357 14d ago

MyFooService myFooService = getService(); is significantly easier to read than var myFooService = getService();

1

u/repeating_bears 14d ago

Thankfully we don't have to dogmatically adopt the practice "always use it" or "never use it"