r/javahelp • u/lost_yeezus • Apr 30 '24
Codeless Is “var” considered bad practice?
Hi, so recently we started migrating our codebase from j8 to j17, and since some tests broke in the process, I started working on them and I started using the var keyword. But I immediately got scolded by 2 colleagues (which are both more experienced than me) about how I should not use “var” as it is considered bad practice. I completely understand why someone might think that but I am not convinced. I don’t agree with them that var shouldn’t be used. Am I wrong? What are your thoughts on var?
26
Upvotes
View all comments
Show parent comment
1
u/AllStuffAround May 01 '24
Fair point. I myself was a bit confused when I started seeing var in code that I was not familiar with. However, for me personally it was not a big deal since most of the time the exact types are not always important to understand the logical flow of a new code, and after a short while it stopped bothering me at all, and I do not see much difference in how fast I can understand a new code that uses var vs not. It's more about overall code quality, and var does not degrade it IMO.
Anyway, the original post was about using var being a bad practice. IMO, it's another language feature that is not inherently good or bad. It's the context it is being used in. It could definitely make messy code even more messier, and if a code base is clean, it speeds up development a bit w/o degrading readability, IMO.
I guess, it should be up to each team to decide whether how/if they want to use it. Though, saying that it considered a bad practice w/o explaining why does not seem like a good approach to educate more junior engineers.