- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Also, unnecessarily passing byval
Admin
I may be repeating the post above me which is currently held for moderation.
Makes me wonder if that codebase started in VB where
On Error Resume Next
was a common (if bad) idiom, and these method-level try with do-nothing catches became the way to retain that (bad) idiom in code during the early years of the conversion.And now they're stuck with it.
Or, far more likely, this is simply the work of over-worked under-resourced abject incompetents. Just like everything else featured on this site.
Admin
Becuase it's VB, it would return False rather than null.
Admin
In my experience, empty catches come from people who don't know what they are doing, but try to take the advice "always catch exceptions". It's bad advice, and the implementation makes the code worse than if they did nothing, but it's still what I hear from the people that I see do it.
Admin
Addendum 2024-03-26 09:34: EDIT: Dang you, markdown!
Admin
In my Stack Overflow answers I always put something in may catch blocks
If you see Swift code with a catch block with a comment in it like that, it was probably copy-pasta'd from one of my SO answers.
Admin
If every function uses this try/catch pattern, then if the function returns an unexpected
null
the caller will ignore it, and errors will be ignored all the way up. If the goal is to prevent the application from crashing with an "uncaught error" exception, it "works".Admin
Swallowing all exceptions? But that's "standard industry practice" :D
The code probably relies on that to pass the tests (if any), so the code seems to work, and only fails in prod, so the manglement cannot easily pinpoint who to blame when horrible things happen.
Admin
Done.
But seriously, what's up with all those naming issues - VS calls them out since 2005 IIRC.
Addendum 2024-03-26 13:45: BTW don't use String.Equals(queryEmail, FnsEmail); in .net you always use the optimized operators before the 40 times slower virtual Equals call that also wastes additional performance with value types because it has to box/unbox them.
Admin
A classic case of paid-by-the-pound code.
Admin
Of course, since emails that differ only in casing are functionally equivalent, the comparison probably ought to be case insensitive. Probably. Depends on how thoroughly the requirements were written (Hah!) and exactly why we're comparing these two fields.
Culture gets a look in, since without knowing where these strings come from we can't guarantee there aren't culture issues too.
So
Return String.Equals(queryEmail, FnsEmail, StringComparison.InvariantCultureIgnoreCase)
is more correct. If we are certain that e.g.QueryEmail
is always non-null that can be more concisely restated asReturn queryEmail.Equals( FnsEmail, StringComparison.InvariantCultureIgnoreCase)
I'll take some exception to MaxiTB's suggestion to always use the
==
and!=
or VB.Net<>
(hope my guess at formatting works; that's lessthan followed by greaterthan characters) operators.See https://learn.microsoft.com/en-us/dotnet/api/system.string.op_equality?view=net-8.0 which states in pertinent part:
So it seems there's not as much deeply optimized mumbo jumbo behind the operators as one might hope. There might be an advantage in the way the compiler susses out the argument types and thereby avoids calling overloads that trigger unnecessary type conversion / coercion. But if so, that's not documented behavior and therefore non-contractual.
And of course if case- or culture-insensitive comparison is required, then
==
or!=
is simply incorrect. Faster but wronger is not an improvement.Admin
One of the often made assumptions about email addresses that are wrong.
Admin
One could make a case for creating a function such as Mailid_compare() to encapsulated Mailid comparisons. It becomes immediately apparent what we are comparing and allows for more complex comparisons - maybe mail aliases are to be considered equivalent, etc.. Even if this is not the case in this particular snippet.
But yes, TRWTF is needless exception handling.
Admin
Back in the dark ages when I was in the role, I used to require teams to provide a "document" of why they needed the try catch...
Admin
Only mostly true for domain names (only definitely true for the ASCII ones) and it's up to the receiving system to decide how to map from incoming mailbox name to where to store the message; that could be done in a case-insensitive way, but there really is no guarantee of that at all.
Admin
Combined with a severe case of copypasta, probably from some documentation (commonly stored in a Word document or Excel table) that says something like "to compare strings, use String.Compare(str1, str2)", so the names str1 and str2 needed to be kept because the documentation said so...
Admin
It perhaps should be noted that "On Error Resume Next" is equivalent to having a do-nothing Try/Catch on every single statement individually. The single catch-all is more akin to an "On Error GoTo" where the target just falls off the end of the function.
There were some specific instances in VBA where it makes sense to use, when it's immediately followed by a check on the current error code with appropriate processing.