Java Glossary : gauntlet

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : G words : gauntlet.

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.

/**
 * Early Return Style Gauntlet
 * to delete a directory
 *
 * @param dirname name of the directory to delete
 * @return true if it was successfully deleted.
 */
boolean deleteDir ( String dirname )
   {
   if ( dirname == null ) return false;
   File dirFile = new File( dirname);
   if ( ! dirFile.exists() ) return false;
   if ( ! dirFile.isDirectory() ) return false;
   // ensure directory empty
   if ( dirFile.list().length != 0 ) return false;
   return dirFile.delete();
   }

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.

/**
 * McCarthy Style Gauntlet
 * to delete a directory
 *
 * @param dirname name of the directory to delete
 * @return true if it was successfully deleted.
 */
boolean deleteDir ( String dirname )
   {
   if ( dirname == null ) return false;
   File dirFile = new File( dirname );
   if ( dirFile.exists() && dirFile.isDirectory()
        && dirFile.list().length == 0 ) return dirFile.delete();
   else return false;
   }

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:

/**
 * Nested If Inline Style Gauntlet.
 */
if ( dirname != null )
   {
   File dirFile = new File( dirname );
   if ( dirFile.exists() )
      {
      if ( dirFile.isDirectory() )
         {
         if ( dirFile.list().length == 0 )
            {
            dirFile.delete();
            }
         }
      }
   }

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.


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 959.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/gauntlet.html J:\mindprod\jgloss\gauntlet.html