PC SOFTWAEWE

Distinctive String Comparisons – The Day by day WTF

As a basic rule, I’ll truly choose code that’s verbose and clear over code that’s concise however makes me assume. I actually do not wish to assume if I haven’t got to.

After all, there’s the category of WTF code that’s verbose, unclear and in addition actually dangerous, which Thomas sends us at this time:

Personal Shared Perform Mailid_compare(ByVal queryEmail As String, ByVal FnsEmail As String) As Boolean
    Attempt
        Dim str1 As String = queryEmail
        Dim str2 As String = FnsEmail
        If String.Examine(str1, str2) = 0 Then
            Return True
        Else
            Return False
        Finish If
    Catch ex As Exception
    Finish Attempt
Finish Perform

This VB .Internet operate might simply get replaced with String.Examine(queryEmail, FnsEmail) = 0. After all, that’d be somewhat unclear, and since we solely care about equality, we might simply use String.Equals(queryEmail, FnsEmail)– which is actually clearer than having a technique referred to as Mailid_compare, which does not truly inform me something helpful about what it does.

Talking of not doing something helpful, there are a number of different items of bloat on this operate.

First, we plop our enter parameters into the variables str1 and str2, which does an amazing job of constructing what’s occurring right here much less clear. Then we’ve the standard “use an if assertion to return both true or false”.

However the actual particular magic on this one is the Attempt/Catch. It is a fairly bathroom commonplace ineffective exception handler. No operation on this operate throws an exception- String.Examine will even fortunately settle for null references. Even when one way or the other an exception was thrown, we would not do something about it. As a bonus, we might return a null in that case, throwing downstream code into a foul state.

What’s notable on this case, is that each operate was carried out this manner. Each operate had a Attempt/Catch that ceaselessly did nothing, or hardly ever (often once they copy/pasted from StackOverflow) printed out the error message, however in any other case simply let this system proceed.

And that is the actual WTF: a codebase polluted with so many do-nothing exception handlers that exceptions grow to be completely nugatory. Errors in this system let it proceed, and the customers expertise weird, inconsistent states as the applying fails silently.

Or, to place it one other means: that is the .NET equal of traditional VB’s On Error Resume Subsequent, which is precisely the form of horrible thought it appears like.

[Advertisement] BuildMaster permits you to create a self-service launch administration platform that enables totally different groups to handle their purposes. Discover how!

About the author

Theme control panel

Leave a Comment