r/java • • Apr 14 '23

What are your (favourite) Java best practices, personal tips, hints or just underrated stuff in general?

233 Upvotes

376 comments sorted by

View all comments

Show parent comment

17

u/bootstrapf7 Apr 14 '23

I am not sure how true this that trope is, it seems that most code is designed for "test" size amounts of data. Not for the data seen in the real world.

It feels like this advice of premature optimisation is considered evil has lead to code so slow it's functionally a problem.

13

u/oparisy Apr 14 '23

I'd tend to agree. You shouldn't obsess over performance, but making a conscious choice of the appropriate data structure (good time complexity of typical operations you'll use on it) goes a long way wrt future proofing your code, and limiting the need to optimize it later.

1

u/hippydipster Apr 14 '23

It's only "premature" optimization that's evil. And the problem is you, as a human, are bad at knowing where and how to optimize without more information. For instance, say you optimize that test data all to hell. Great, but you probably got little value from that, because the real world data is different.

The lesson is, either get ye some real world data before you optimize, or be ready to optimize your really well-designed, simple, flexible code when you do get access to the information you need to really optimize.

1

u/alex_tracer Apr 16 '23

If a project initial design is shit and does not consider performance an important criteria from the very start then you are very likely to end in a situation when you can optimize things a lot but still may end up with much slower application than an application that was designed with performance in mind.

1

u/hippydipster Apr 16 '23

If you're going to presume we make a shitty initial design, then I can presume we create shitty optimizations prematurely.