java - A Boolean'ian Quandary -
consider following java method:
public boolean compare(string val1, string val2) { return val1.length() > 0 && val1.equals(val2); } if isn't obvious, method first checks if val1 length greater 0 (ie. have compare) , applies .equal() method.
of course, quandary comes because if val1.length() isn't greater zero, method return false. bad because becomes unknown false came from; val1.length() method or equals() method.
a rewrite should fix problem:
public boolean compare(string val1, string val2) { if (val1.length() > 0) { return val1.equals(val2); } return // oh dear.... stuck again ! } the obvious problem here java methods require method return return type specified. in case return true or false (actually, because i've specified boolean instead of boolean [capital b vs lower b] can return null). return here? turns out true or false won't work either, because again if val1.length() false, whatever return value what's returned, leading again possible false positive or false negative. although can set null here, calling code generate nullpointerexception -- no null.
what need not boolean, trilean! so, devised this:
// trilean.java public enum trilean { false, true, ignore } and re-wrote method so:
public trilean compare(string val1, string val2) { if (val1.length() > 0) { return (val1.equals(val2)) ? trilean.true : trilean.false; } return trilean.ignore; } and works:
if (compare(val1, val2) == trilean.true) { // stuff } this logic (btw) becomes valid when example want perform regex on string, string regex rules demand begin letter or number, want run regex if string isn't empty. take example servlet, may have html input field not required. testing these rigid rules "^[a-za-z0-9][a-za-z0-9]*$" cause val.match() method return true or false, hence breaking form field validation, on non-required field.
so,
public boolean validatefield(string data) { return data.length() > 0 && data.match("^[a-za-z0-9][a-za-z0-9]*$"); } would generate same false positives or negatives.
question is: have been approaches quandaries this? how can improve logic while sticking boolean?
the question trying tell? , responsible this.
in case not true , false in result. because asking compare (when ask isequal case different). more natural result options are:
public enum compareresult{ equal, unequal, empty } note renamed trillian actual type means, namely result of (somewhat special) comparison. if want precise say: equal_non_empty, indicates users of function isn't normal comparison, because empty string treated special case. in same way should name function indicate handles empty strings in different way, instance comparestringsifnotempty. long , clumsy name, indicates function has special it.
i calling function making programming mistake when passing empty string (because should have validated it's user input). in case should throwing unchecked exception. 'illegalargumentexception' common type throw in case.
finally if interested in in few placed place, might possible limit scope of function, because behaviors not using might expect.
so in all: think people use function in future. expect name? might wrong if don't read documentation properly. how can avoid these mistakes?
Comments
Post a Comment