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"); 

Pattern p = Pattern.compile(".*[Tt][Rr][Uu][Ee].*");
Matcher m = p.matcher(value);
return m.matches();

        } catch (NullPointerException npe) {
            
        } catch (ClassCastException cce) {
            
        }

        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.

[Advertisement] Otter – Provision your servers automatically without ever needing to log-in to a command prompt. Get started today!

Remy Porter

Source link

You May Also Like

After only one week on the market Ben and Jerry discontinues their Trump Banana Bigot ice cream

The biggest ice cream maker in the world, Ben and Jerry, has…

45 Things Folks Online Thought Were Just Rumors Only To Find Out They Were Real All Along

Rumour about a local businessman who tried to end it all and…

Bad Boys, Bad Boys.,, : People Of Walmart

Bad Boys, Bad Boys.,, : People Of Walmart …

Paper Trail – People Of Walmart

We all know what it’s like to walk out of the bathroom…