Mario's predecessor was writing some Java code that parsed textual input. Thus, it needed to be able to turn the string "true"
into the boolean value true
, but also needed to handle "True"
and "TRUE"
.
Now, in Java, the obvious answer would be to use the equalsIgnoresCase
String member function. if (value.equalsIgnoresCase("true"))…
. I mean, obvious to you or me, anyway.
But if you're a developer with a hammer, you're going to turn every problem into a nail. And when that hammer is regular expressions, you're going to turn every problem into two problems.
public boolean isHelpReference() {
try {
String value = mSourceParams.get("CONTEXT_HELP"); //$NON-NLS-1$
Pattern p = Pattern.compile(".*[Tt][Rr][Uu][Ee].*");
Matcher m = p.matcher(value);
return m.matches();
} catch (NullPointerException npe) {
// do nothing
} catch (ClassCastException cce) {
// do nothing
}
return false;
}
Indentation is as submitted.
Again, the goal here is to turn a string containing "true" into a boolean value, but this actually has the bonus of turning any string containing "true" as a substring into a boolean true
, including "not true". So it's incorrect, but just so happens to work because there are no input strings like that.
I also think the ClassCastException
is mysterious, and unless mSourceParams.get
throws that out, I don't think that's a real expected exception here. And if mSourceParams.get
does throw a ClassCastException
, I think there's a deeper problem.