CodeSOD: TrUe Romance

This post was originally published on this site

The Daily WTF

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

To read the full article click on the 'post' link at the top.