- 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
Wow, that has to be the oldest "classic WTF" I've seen yet! Also, with this WTF being at least 11 years old now, I'm starting to feel old a little myself.
Admin
Yeah, quantum. I'm just so glad nobody thought maybe these are functions with a very restricted "domain" (set of inputs).
They are really
hexdigit2bin
andhexdigit2int
. And of course they return something weird on out-of-domain input. That's a bit WTF, but only a bit. (I'd assert or throw, but ...)They are more "er, ok, but..." rather than real WTFs.
Admin
Returning '2222' from
hexdigit2bin
is a wtf no matter how you look at it. I don't usually expect my binary strings to have random noise inserted for whatever reason.Admin
"I'd assert or throw, but ..."
And what exactly should it return if I pass "W" or "65"?
Admin
FILE_NOT_FOUND obviously
Admin
Binary is base-2, so "2222" seems very appropriate. No WTF here at all...
Admin
Your inclination to throw an exception was entirely correct.
This is burying the error. (The case of nulls is what Tony Hoare called his one billion dollar mistake.)
You had an error condition and instead of raising it right away so it could be dealt with, you stored it somewhere in a data structure. That stack trace is gone, so solving the problem just got much, much harder.
That data will get passed from component to component. It could be written into a file or stored in your database, possibly overwriting valid data with corrupted data.
At some point, you'll have to debug it. That's when you'll spend hours and hours cursing "WTF did this come from?"
Admin
"I've never seen code like this"
Admin
If you don't expect your binary strings to have random noise, don't give this function hex strings with random noise.
Making wrong output obviously wrong is important.
Admin
I don't expect my binaries to be passed around as strings. That is TRWTF.
Admin
This is called proper defensive programming. You expect that somewhere along the way due to a bug or input error a hex string with a wrong value may be passed. So in case that happens you throw/exit with error, so that the issue can be immediately noticed right where it occurred. Silently passing (intentionally) horribly wrong results further down the line is how you get the weirdest bugs.
Admin
I suspect the programmer didn't want to see the warnings in VisualStudio.NET that not every code path returns a value.
(In C#, this would even be an error that would prevent compiling.)
Returning something nonsensical instead of throwing an exception ist still a WTF.
(I personally prefer to define a class UnexpectedAlternativeException)