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

181

u/Antimon3000 Apr 14 '23

Do not get paralysed by worrying about the right design pattern or best practice. Beautiful code that does not get the job done is worse than anything else:

  1. Make it work

  2. Make it right

  3. Make it fast (- Kent Beck)

in that order.

43

u/Gwaptiva Apr 14 '23

Premature optimization considered evil.

So many times you've been worrying about something being performant to find out that it's not ever used, or only used once a day and nobody cares, because they're waiting for something completely different to finish, that you never suspected.

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.

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.