- Feature Articles
- CodeSOD
-
Error'd
- Most Recent Articles
- Secret Horror
- Not Impossible
- Monkeys
- Killing Time
- Hypersensitive
- Infallabella
- Doubled Daniel
- It Figures
- 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...
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.
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() ...
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.
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.
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
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?
Admin
Company closes for the day and everybody goes home?
Admin
Well, we don't know the whole story. It is quite common in business to have combined numbers, so for example a product segment code, the order date plus an index - so a four digit index could be more than enough. Would also explain the leading zeros cause as we all know, it's best practice to eliminate leading zeros - but never say that out loud with a line manager in the room.
Admin
The cast to coerce the return of
.toString().Trim()
(which, logically, should be astring
) tostring
makes my eye twitch...Admin
Looks to me like unclean refactoring. I wouldn't be surprised if there was a hard cast before because the value is actually also a string in the DB. Then someone used ToString() instead of the proper GetString() to add the Trim() and was to sloppy to remove the now redundant cast. However, maybe it's not even a DbDataReader but something completely different - it's very hard to guess what is even going on by those limited copy examples.
Addendum 2024-12-16 17:37: copy examples ? More like code samples - my cue to get back to coding :-)
Admin
With numbers maybe. But this is definitively not a number. So much is obvious from the code. It's a string that happens to be composed of numerical digits. And for those, leading zeroes can be both a requirement and are often just plain useful precisely to ensure nobody can store it as a number and do number things with them.
Admin
I think it might have started with =(string)dr["TransactionOrder"]; then the requirement came to add a trim, and =(string)dr["TransactionOrder"].Trim() didn't work, so they added an extra .ToString()