- 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
Slightly disappointed the code doesn't use the classic# switch-fall-through (goto 2, goto 3, if I'm not mistaken.)
Admin
Please provide an alternate implementation that allows one to se a breakpoint when 2 leading characters need to be added...
Edit Admin
Beautiful example of enterprise-level code, including the unnecesary cast to string in the first line. The presence of a DataRow might indicate this is an attempt to somewhat sanitize user input (best case case scenario) or just to deal with poorly formatted data from a database, or maybe both.
Edit Admin
Ehm, honestly not sure how to make this a lot better because we know little about the DB in question. At least I guess that dr stands for a DbDataReader, so the first ToString() is required but using the actually type would be better. Maybe it's a float, maybe a int, maybe some weird Oracle type - who knows. It could also be a string, not clear from only this bit of code. Now if you have the string I don't see how you would implement the switch otherwise, sure you could use a switch expression but that's it. Using a if else if chain is definitely less clear, using a reference type is obviously wrong too for a 3 case example (those only make sense with 10+ cases).
So TLDR, the only WTF here is pretty much not using an the correct type specific method of the DbDataReader (https://learn.microsoft.com/en-us/dotnet/api/system.data.common.dbdatareader?view=net-9.0).
Admin
Well, there's String.PadLeft() ...
Edit Admin
If that is a database connection, reading the value will give an opaque 'object', not the properly typed 'int' or 'long' that 'ToString("d3")' could be used with. Even then, though, they could 'PadLeft('0', 3)' the string instead of using the switch statement.
Edit Admin
Yes, we've seen it many times . . . but I admire the nice touch of assigning a variable to itself in the "default". The cherry on the top of this Unnecessary Sundae.
Edit Admin
You can just use a conditional breakpoint, like a normal person.
Admin
I'm sure the true WTF programmer could have found a way to incorporate Duff's device into this
Admin
still better than
npm install left-pad
Edit Admin
In C# fall-through doesn't require a goto, because it's 100% safe by itself:
However there is a transfer control statement
goto case
for non-fall-through scenarios:Admin
9999 TransactionOrders, and then?