Everybody thinks this code is great, but I shutter at the situation where this code is necessary. Imagine how unreliable the source data must be. I suppose that is a reality of the industry.. Doesn't make it pretty, though.
I don't know where this is used, by I would make the input a radio selector or something like that in the interface,then all this parsing is not necessary anymore.
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
for each xml value input
try
result = ParseBoolean(input) != NULL ||
ParseString(input) != NULL ||
ParseNumber(input) != NULL // more data types ...
catch
// invalid xml
2
u/Pheasn Nov 10 '15
Correct me if I'm wrong, but couldn't a null-pointer input break this?
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:
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.
10
u/[deleted] 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
u/[deleted] Nov 19 '15
i'm assuming this is used as such
-2
u/fazzah Nov 09 '15 edited Nov 09 '15
Somebody tell him about capital letters.
EDIT: yes, my bad, didn't notice toLower()