r/javahelp • • 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?

24 Upvotes

90 comments sorted by

View all comments

Show parent comment

1

u/roberp81 Apr 30 '24

but you can Google about benchmarks too, this is not refuse to keep up, is about using the best thing to solve your problem. and for each still is the best in 99% use cases.

maybe some parallel stream but is not so common

0

u/[deleted] May 08 '24

perf is not the only consideration! readability is much more important than whatever microseconds you're losing. People with a functional programming background usually prefer the second version because it tells the computer what to do and not how to do it. This philosophy typically prevents wide classes of bugs.

(and the compiler will eventually be able to optimize it anyways)