24
u/canada_mike Nov 09 '15
you really do have to ensure you cover every possibility. The word 'true' isn't always the only way to describe a condition that is not false. Some people say 'yes'. Computers say 1.
I would improve this method by adding several other languages, after all the german 'nein!' or the russian 'nyet!' will break this. Since text inputs are the best inputs you really have to anticipate what the user will provide you as an answer.
two examples of this, straight out of the simpsons:
Homer, working from home, TRIPLES his productivity by typing 'Y' instead of 'YES'. TRIPLE! Now that is a great result.
When filling out a form, homer writes "ok" in the box marked "do not write in this space". Is he wrong? Probably! But he is the user so we have to anticipate this type of boobery.
In conclusion, booleans are hard. Don't assume it's binary
42
u/mellowmymints Nov 09 '15
There's nothing wrong with this.
I mean, I would return immediately in the case block, but otherwise this is a clean beautiful piece of code.
13
u/homesarstar Nov 09 '15
I know! I feel bad since this looks like what I would do, and I swore I'd reevaluate my education when I saw something on here that I'd write.
2
10
Nov 09 '15 edited Nov 09 '15
Whats funny is that this is how PHP's boolean comparison operator probably works underneath.
5
u/gigimoi Nov 09 '15
There's nothing that wrong here. It's not localized, but that might not be a relevant issue, and it can easily be changed to localized.
1
Nov 19 '15
i'm assuming this is used as such
for each xml value input
try
result = ParseBoolean(input) != NULL ||
ParseString(input) != NULL ||
ParseNumber(input) != NULL // more data types ...
catch
// invalid xml-2
u/fazzah Nov 09 '15 edited Nov 09 '15
Somebody tell him about capital letters.
EDIT: yes, my bad, didn't notice toLower()
9
u/BackwardsBinary Nov 09 '15
input.ToLower()Line 4 of OP's code. I'm gonna go ahead and guess this parses the input into lowercase.
1
1
2
u/Pheasn Nov 10 '15
Correct me if I'm wrong, but couldn't a null-pointer input break this?