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.
/** * 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 * 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. */ 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.
home |
Canadian Mind Products | |||
| 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 | |||