Outstanding String Comparisons – The Daily WTF

As a general rule, I will favor code that is detailed and clear over code that is concise but makes me think. I really don’t like to think unless I have to.

Of course, there is a class of WTF code that is verbose, obscure, and also really bad, which Thomas today he sends us:

Private Shared Function Mailid_compare(ByVal queryEmail As String, ByVal FnsEmail As String) As Boolean
    Try
        Dim str1 As String = queryEmail
        Dim str2 As String = FnsEmail
        If String.Compare(str1, str2) = 0 Then
            Return True
        Else
            Return False
        End If
    Catch ex As Exception
    End Try
End Function

This VB .Net function can easily be replaced with String.Compare(queryEmail, FnsEmail) = 0. Of course, that would be a little vague, and since we only care about equality, we could just use it String.Equals(queryEmail, FnsEmail)– which is honest clearer but to have a method called Mailid_comparewhich doesn’t really tell me anything useful about what it’s doing.

Speaking of doing nothing useful, there are a few more tidbits in this function.

First, we put our input parameters into variables str1 and str2, which does a great job of making what’s going on here less clear. Then we have the traditional “use an if statement to return true or false”.

But the real special magic is in this one Try/Catch. This is a pretty bad standard useless exception handler. No operation in this function throws an exception- String.Compare they will even gladly accept null references. Even if an exception were somehow made, we would not do anything about it. As a bonus, we’d return null in that case, throwing the downstream code into a bad state.

What is significant in this case is that every function is implemented in this way. Every function had a Try/Catch that often did nothing or rarely (usually when they copied/pasted from StackOverflow) printed an error message, but otherwise just let the program continue.

And that’s real WTF: a code base polluted with so many exception handlers that do nothing that exceptions become absolutely worthless. Bugs in the program allow it to continue, and users experience bizarre, inconsistent states while the application silently crashes.

Or, in other words: this is the .NET equivalent of classic VBs On Error Resume Nextwhich is exactly as terrible an idea as it sounds.

[Advertisement]

BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Find out how!

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *