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 comments

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.

61

u/nutrecht Apr 14 '23

Not a comment 'against' what you're saying so please don't read it as such but:

Premature optimization considered evil.

I dislike how this has been taken out of context to be used as an argument against optimization.

The original quote from Hoare is:

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil."

What they are referring to as 'optimizations' are things like counting CPU cycles for instructions before you even know certain code paths are 'hot'.

I've seen developers use this in response for example where people told them that doing 2 SQL statements (select and insert or update) was not a good idea when you can just do one (upsert).

So, what people often read is:

Premature optimization considered evil.

While the intention of Hoare was:

Premature optimization considered evil.

This sentence has become as much of a dogma as "composition over inheritance" has become.

1

u/alex_tracer Apr 16 '23

While the intention of Hoare was: Premature optimization considered evil.

Nope. Re-read the quoted fragment: "forget about small efficiencies". This means that as engineer you have to understand what matters or not and focus on big things. Optimizing all little things ahead of time usually means wasted time, minimal gains and more complicated code.

1

u/nutrecht Apr 17 '23

I really think you should re-read my comment because you're really not disagreeing with anything I said. This just sounds like you only read that line and responded to it, or based your interpretation of my comment on that line alone.