gauntlet : Java Glossary

gauntlet
A gauntlet is a series of conditions guarding an action. There is usually only one fail action (possibly empty) and one success action (possibly empty) for the series of tests as a whole.

Early Return Style Gauntlet

Writing gauntlets the obvious way in Java soon wraps you N layers deep in nested ifs. One of the easiet ways to write gauntlets is to make them a separate procedure that returns either success or fail. You use the early return feature to avoid the deep nesting.
The above code is an artificial example since File.delete is defined to return true if and only if the file denoted by this abstract pathname exists and is a directory; false otherwise. In other words, it does most of the checks itself, and so they are not necessary.

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.

McCarthy Style Gauntlet

If your gauntlet is composed purely of conditions, you can use && or || to join them in a single if.

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.

Nested If Inline Style Gauntlet

Java has no nested methods. Therefore all the information your gauntlet method needs must either be passed as parameters or available via instance and static variables and methods. Sometimes the volume of information you need to pass via parameters is so high, it makes writing a separate gauntlet method not worth the effort, and it is cleaner to make do with an inline gauntlet implemented as a deeply nested if like this:

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 into a 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.


CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/gauntlet.html J:\mindprod\jgloss\gauntlet.html
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, please quote it and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. Threatening to kill me or spouting obscenities has yet to persuade me to change my mind.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.211]
You are visitor number 13,034.