Java purists dislike using the early return, even in a small method like the one above. I like this style because the conditions are independent and uniform. You can shuffle the order easily and add new conditions without having the adjust the existing code.
Another common type of early return gauntlet has a void return. If any test fails it quietly returns without doing anything. Another type just returns a boolean without executing the goal action, leaving that up to caller.
One problem with the technique above is things can get a little complicated with precedence and all the possible combinations of !, || and &&. The other problem is you have to break the pattern any time a condition needs a little precalculation.
If find the nested if style above the hardest to maintain. When it comes time to add another condition or reorder the conditions it turns in to confusing sea of { and }. The other problem is the gauntlet in not clearly delimited as a logical unit. It flows seamlessly into the logic before and after.
![]() |
and suggestions to improve this page to Roedy Green : | ||
| Canadian Mind Products | |||
| mindprod.com IP:[65.110.21.43] | |||
| Your face IP:[38.103.63.17] | The information on this page is for non-military use only. | ||
| You are visitor number 8,727. | Military use includes use by defence contractors. | ||
| You can get a fresh copy of this page from: | or possibly from your local J: drive (Java virtual drive/Mindprod website mirror) | ||
| http://mindprod.com/jgloss/gauntlet.html | J:\mindprod\jgloss\gauntlet.html | ||