- 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
Did the original code have an empty Try block? Well, that would be safe, because nothing would be executed. Personally, I would build the Catch block as:
Admin
Ran the following code against today's article, glad I put the try catch in here because it could have crashed without it!
Try MakeFunnyArticle(joke,humor) Catch Ex as Exception Msgbox("UNFUNNY ARTICLE!") End Try
Admin
The business logic was temporarily removed because it was not handling properly the FizzBuzz case
Admin
This is the code equivalent of "I once caught a TimeStamp this big!"
Admin
Sure. That's self-documenting code. I mean:
A reader knows exactly what the length means. While:
Who knows?
I probably would have written:
but arguably that's inferior, because a later programmer could come along and change the length of the string -- maybe add the time of day or add an AD/BC indicator or whatever -- and change the assignment statement but not change the comment. So now it says
Yeah, that would be dumb, but programmers do it all the time.
But of course taking the length of a string of spaces tells me less than dateLength=10. Now I have to count the spaces. What did that gain?
Admin
Or this code was written before the @deprecated tag was added to Java. Or, let's be fair, maybe the programmer isn't familiar with @deprecated.
RE just delete the function and get the compiler errors: But what if this function is in a library that might be called by many programs? The programmer wouldn't necessarily know all the programs that use this library. So if he just deleted the function, then six months later somebody recompiles and gets this mysterious not-found error, and he has to figure it out. If that's the case, sure, it would be better to put in a comment explaining why the function is obsolete, but I don't think this solution is totally idiotic. Not the best thing to do, but not totally idiotic.
Admin
What gets me about this one is that he used the length member right there. I understand if someone writes his own function to do something even though there's a built-in function in the standard library. So okay, he didn't know it was there. Ignorant, but not stupid. But this ...
Ok, maybe everyone else is saying, Duh, that's why it's on thedailywtf and not on "the daily oh by the way did you know there's a better way to do that".
Admin
Admin
Not all langauges have compilers/statick analysis tools. It may be that the class has to meet requirements of some interface, which are actually too restrictive, and the programmer is 99% sure, that a given function will not be called, but still prefers to have a nice stacktrace in case it happen.
As to the string of spaces, perhaps it is a buffer used to compose the characters of the date one by one, in which case initializing it with spaces makes some sense.
Using if(true) is either a way to make compiler happy (which in turn indicates existence of some static analysis tools...) or generated by some makro preprocessor which replaces some placeholders with boolean literals, or just a way to make it obvious to the reader that this line is "special".
Counting using loops is very common among people who just started to learn programming and try to encode what they would do if they were tasked with the same problem the computer is. Some of them are not smart enough to realize that the "for mantra" actually contains subexpression which already is the answer to their problem. Also, there might be people for which corner cases of empty array seem to be "better" handled this way. But, the most probable explanation is that the loop used to contain some more conditions/rules.
Admin
*) An attempt to explain the for-loop construct: With AI on the rise, there is less and less reason to treat structures of silicon other than the human work force. So the developer imagined him/herself rather a manager of the silicon work force. This is not simple code, it's the epitome of wise management as lived for decades!
Admin
Well, to be fair , he tried this first:
and when he realized that wouldn't work, consulted the documentation and realized he could replace countDocuments() in his code with user.documents.length and Bob's-yer-uncle.
Admin
I disagree. While I think you're on the right track I think it's more about finding use cases that actually trigger the code. If you just want to find the calls remove it and see what fails to compile.
Admin
And neither will be as slow as using both.
Admin
public DataModel getEditionModel() { throw new UnsupportedOperationException( "You shouldn't be here" ); return null; // just in case! }
Admin
int characterLengthOfProperlyFormattedDateString = 10;
Admin
unction countDocuments() { return
user.documents.length == 0 ? 0 : user.documents.length == 1 ? 1 : user.documents.length == 2 ? 2 : user.documents.length == 3 ? 3 : user.documents.length == 4 ? 4 : user.documents.length == 5 ? 5 : user.documents.length == 6 ? 6 : user.documents.length == 7 ? 7 : user.documents.length == 8 ? 8 : user.documents.length == 9 ? 9 : user.documents.length == 10 ? 10 : user.documents.length == 11 ? 11 : user.documents.length == 12 ? 12 : user.documents.length == 13 ? 13 : user.documents.length == 14 ? 14 : user.documents.length == 15 ? 15 : user.documents.length == 16 ? 16 : user.documents.length == 17 ? 17 : user.documents.length == 18 ? 18 : user.documents.length == 19 ? 19 : user.documents.length == 20 ? 20 : user.documents.length == 21 ? 21 : user.documents.length == 22 ? 22 : user.documents.length == 23 ? 23 : user.documents.length == 24 ? 24 : user.documents.length == 25 ? 25 : user.documents.length == 26 ? 26 : user.documents.length == 27 ? 27 : user.documents.length == 28 ? 28 : user.documents.length == 29 ? 29 : user.documents.length == 30 ? 30 : user.documents.length == 31 ? 31 : user.documents.length == 32 ? 32 : user.documents.length == 33 ? 33 : user.documents.length == 34 ? 34 : user.documents.length == 35 ? 35 : user.documents.length == 36 ? 36 : user.documents.length == 37 ? 37 : user.documents.length == 38 ? 38 : user.documents.length == 39 ? 39 : user.documents.length == 40 ? 40 : user.documents.length == 41 ? 41 : user.documents.length == 42 ? 42 : user.documents.length == 43 ? 43 : user.documents.length == 44 ? 44 : user.documents.length == 45 ? 45 : user.documents.length == 46 ? 46 : user.documents.length == 47 ? 47 : user.documents.length == 48 ? 48 : user.documents.length == 49 ? 49 : 50; }
Admin
Oh come on, wake up, stupid. My folding meter stick also has imperial measurements on it in case I bump into American mathematicians arguing over the height of a tree. You don't need it for European ones because they're clever enough to determine tree height without needing a physicist to chop it down for them.
Admin
Admin
So how is "You shouldn't be here" any more enlightening?
Admin
It's obviously 135 hands!
Admin
Admin
Admin
You say that like it was a bad thing.
Admin
Admin
Okay, I'll buy that. There's just one question that remains: Why does it still return editionModel? I mean, do we still need to return that decommissioned value as opposed to, say, null?
Admin
Why does it return them though?
Admin
Admin
You're right on that one... the version "if(true)" will cause a warning from an IDE, but isn't actually a compiler error as the version above would.
But you're wrong on that one... this is perfectly valid Java code, though normally seen with UnsupportedOperationException instead. A function must return a value only if it doesn't throw an exception...
Admin
A good start, but no guarantee. It's a "getter" method, so there's a very good chance it's called via reflection, from some reference to an "editionModel" property in an XML file or something...
Admin
That WTF is like an Ogre... there're just WTFs layered on top of WTFs. And one of them is what you say: which is that even if you just look at that code and don't use any language features not used there, there are still much better ways to write it.
Admin
I've never really been fond of the throws keyword.
Admin
You introduced a bug there...
Admin
Admin
I think I know what is going on here.
Codemonkey programmer only seen use of the array.size / rows.count propperties used inside a for loop, never in a direct assignment. So he/she thinks to be clever and does the for-loop "trick". Wow....
Complete lack of understanding of the fundamentals I reckon.
Do you see the pattern, too?
Admin
FTFY
Admin
For the last WTF, the joke isn't that they're throwing an exception right after catching one, but that the try block is empty. I assume they either deleted whatever was in the try block, or they wanted to put the try block around every call to the method but couldn't figure out how.
Side-note: If throwing an exception right after catching one (to change it to a more descriptive message), you should reference the first exception in the constructor of the second, so that its stack-trace information of the first is kept (otherwise when the exception comes up, it only points to where you threw the 2nd exception, and you can't check where the original error came from). Here's an example: http://blog.codinghorror.com/rethrowing-exceptions/
Admin
Admin
Because no one did it: "Ohhhhhh, sweet mystery of life at last I found you!"
Admin
Haha! WTF!
Admin
Which would be wrong, because when multi-threading those aren't necessarily equivalent.
In C# length could be a property which returns a different value each time even when single threading.
Admin
He also may have anticipated the need to filter out some of the items in that array and left that function as an easy-to-find hook to install the feature when the time comes. Frankly, I've written code like that, and at times it has been rewarding. I think the fact that he used the length command indicates it was intentional.