MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/java/comments/12lmj78/what_are_your_favourite_java_best_practices/jgb7v87/?context=3
r/java • u/trecatorsirabdator • Apr 14 '23
376 comments sorted by
View all comments
22
Use precondition ifs to avoid spaghetti nesting.
If doesnt meet conditionReturn 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
1
Also known as guard clause, also remember you can just return; if the method is void
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