- 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
I've been in a place where early returns were considered the epitome of evil. that's how you end up with code like this.
Admin
The problem is how to persuade the Powers That Be, and Those Who Decide (hopefully the same people!), of this. As I've noted before, this is something you have to construct as an argument in Businessese rather than in Engineerian - that is, an appeal based on the costs in time, in work lost (and therefore time lost to redo it), and so on. Fuss about the quality of the code later, but start by getting source control in operation to eliminate (er, reduce) the need for rework.
At least the code is clear: if all these fields are filled, process the object, otherwise whine in a message box about the first missing field.
Admin
The filename Custom.vb is terribly misleading. It should be something like SourceCode.vb - how else could you differentiate it from invoices (Invoice.xls) or reports (Report.doc)?!
Admin
Yeah, we should let the AIs do the coding! :( DOH!!!
Admin
Imagine using a company purchased subscription to ChatGPT or the lot to compose code.
Brand new dimensions for TheDailyWTF!
Admin
Eh, you can work around that with minor effort, assuming that convincing the other person is a lost cause:
string message = "";
if (condition1) { message = "problem1"; }
if (message == "" && condition2) { message = "problem2"; }
...
if (message == "") { do stuff; } else { display message; }
return;
Admin
Early returns? bad bad BAD . do you not remember the early return headline "Dewey Wins" ?
Admin
The actual headline was "Dewey Defeats Truman", but what's usually left out is the defeat was in a swimsuit competition, not the election itself.
Admin
Not really. This code uses early returns and lots of levels of nesting. What do you think all those
Exit Sub
statements are doing?Admin
Doesn't a default MsgBox just have an OK button? What is DefaultButton2?
Admin
Even without early returns, you could fix the nesting issues by simply flipping around the true/false branches and using ElseIf.
Admin
The "single return statement" rule is the epitome of cargo cult. Having seen some convoluted horrors with return statements buried in many layers of loops and if statements, I get how that rule came about, but practically always, rewriting a hard-to-follow function with multiple returns to conform to the rule without some additional refactoring just makes it even more convoluted and hard to follow.
Admin
Teams that don't use version control absolutely should turn AI loose on their code IMO.
Admin
From what I remember the "single return" rule comes from languages that don't have structured error handling, so you'd often need to run cleanup code at the end of a function, regardless of whether there was an error.
Hence, single return: Call functions, set values. If something goes wrong, set some error flag and release resources. And in order not to duplicate and inconsistate* the cleanup, return only at the end of the function. Maybe even throw in a "goto cleanup" to avoid deep nesting as seen here, without the (probably anyway irrelevant) performance overhead of the pattern of repeated "if not error and actual condition" blocks.
* I don't think this word exists, but it should.
Admin
Brother you are trying to argue against a bunch of... who think they know more than everyone else. Clearly most people commenting have absolutely no knowledge of what they are complaining about.
Admin
The code looks like a WTF factory that was translated from VBA-heritage VB. In addition to the nesting, we have the following:
Object
as the type of the loop iterator instead of the actual type or an interface, which leads to...Option Strict
is offIs
check withString.Empty
, more likelyString.IsNullOrEmpty
would be better (though I'll give them credit for usingIsNot
)MsgBox
functionAlso, a small thing, but a
ContainsKey
check suggests that they should possibly be using aTryGetValue
type of call instead (probably there's enough going on that the double-lookup in the dictionary isn't actually worth worrying about).Admin
Per the docs: "Indicates that the second button from the left is selected as the default button when the message box appears." (All the possible values for the MsgBoxStyle Enum are also described in a handy-dandy little table )
Not sure what that does when there's only one button...