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

22

u/ImTalkingGibberish Apr 14 '23

Use precondition ifs to avoid spaghetti nesting.

If doesnt meet condition
Return early
// main logic

Is better than:
If condition
// lots of lines inside the if block

1

u/[deleted] Apr 15 '23

Also known as guard clause, also remember you can just return; if the method is void