- 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
Let's get these out of the way:
Brillant! He shoud have used if(IsTrue(milliDiff==0))! The real WTF is (insert lame callback here).
Admin
I don't get how making it positive then negative again is any easier than just doing it with the negative value.
Admin
I bet that took him -1029 millseconds to write
Admin
I like this:
Admin
Is there a prize for when the number of WTF's exceeds the number of lines of code?
Nice to see the function accounts for the posibility of negative milliseconds and minutes. Must be used in conjunction with the flux capacitor.
And I don't know how many times I've forgotten to watch out for the exceptional value 0. Maybe this comment will help me remember.
Admin
I wish I could code that well, maybe I would make more money!!!!
Admin
I suppose he is a little confused about what "divison by zero" really means. The last time I checked dividing zero by any number always returns zero, so the extra check here is not necessary.
Admin
He's not avoiding that. He's avoiding a bug that showed up in .NET (and hasn't been fixed!) whereby if you divide zero by a number, you keep getting zero, instead of a DivisionWithZero exception. I attempt to avoid such problems in my code by writing only Javascript.
Admin
I like how the comments on this site are usually bigger WTFs than the posts themselves.
Admin
Actually, if you divide by zero a DivisionByZero exception will be thrown in C#, something similar in Java and IIRC C++ will throw an exception aswell
Admin
I'm still trying to figure out when this would be used. My guess is that he's subtracting two different time values (anyone want to wager how many lines he took to do that? how many type conversions?) then converting that difference to minutes. But any system that measures and stores enough milliseconds to make subtractable minute values is borked up enough as it is. At that point you might as well meaure fractional minutes (5.25 minutes = 5 minutes, 15 seconds).
Admin
You might want to re-read his comment. Try turning your sarcasm detector on also.
Admin
I don't get it Anonymous. What's wrong with what Anonymous said? Anonymous is right.
Admin
Not always true that 0/x = 0: x could be 0 as well. And if these were float/double values, x could also be NaN, +inf, -inf, or -0.0.
Of course, this doesn't change the fact that there is nothing exceptional about 0 in the WTF code.
Admin
Sarcasm escapes some people...
Admin
0 / n = 0 // This is NOT a bug in .NET, this is valid math!
n / 0 = DivideByZero Exception
In this case his math should be; milliseconds / 60000 = minutes. If milliseconds is 0 then minutes will be 0 (try it if you don't believe me)
Admin
This is true, but I meant it in the more general sense that dividing zero is the same as dividing any other number. You still have to look out for division by zero, etc.
Admin
What's sarcasm?
Admin
I don't believe you! I'm joining up with Dave Markle and sticking with Javascript from now on! Screw .NET!
Oh by the way, everyone knows the example isn't valid C# anyways cuz the coder mispelled "bool"! LOLOLOL!
Admin
And where do I find a detector for it?
Admin
Comic Book Guy: A sarcasm detector, that's a real useful invention.
KABOOM!
Admin
"Nice to see the function accounts for the posibility of negative milliseconds and minutes. Must be used in conjunction with the flux capacitor."
From the name of the passed parameter, it looks like he's expecting a difference of two timestamps, which could well be negative.
Possibly he converts to positive to get a consistent round-towards-zero regardless of the sign of the input, rather than floor rounding, as well? Maybe this isn't quite as WTF as it looks at first glance.
Admin
Why worry about division by zero in this example anyway?
Admin
That's exactly the point. This guy was writing the code and thought, "oh man, I better be careful not to divide by zero" and so put in this crazy check. I guess he didn't understand the difference of divison by zero as opposed to divison of zero.
Admin
Me too. There seems to be a prevalance of dumb people with no sense of humour who read thedailywtf.com. My request to these dumb people, is please keep posting, I enjoy laughing at you.
And the best thing is that the dumb people i'm talking about don't even know i'm talking about them.
Admin
Possibly because he wants to round towards zero rather than to the floor. That is -2.3 minutes should round to -2 rather than -3.
Admin
Well, nothing wrong with handling those exceptionals of negative differences and 0. But what if the difference in milliseconds is evenly divible by 617? Isn't that exceptional too? He needs to divide the milliseconds by 2 then multiply the resulting minutes by 2. Or is it divide and multiply by 4? Help me out here.
And there's so many other exceptionals that could happen. What if the number of milliseconds happens to equal the number hours since I was born? Wouldn't that be exceptional? I don't see an if block for it.
Oh, and rounding those milliseconds to the nearest (int) minute? That's just for dweebs who don't get Web 2.0.
--Rank
Admin
Actually I could see somebody writing something like this if they were for some reason worried about the rounding behavior of division with negative integers. e.g. if he were concerned that (-11)/3 might be different than -(11/3) and that difference was important for some reason. It's a stretch I'll admit.
The check for zero looks like an extremely common case of premature optimization. He knows what the answer is so he avoids the overhead of doing the divide.
Admin
As far as I know, integer division does not necessarily use a floor function to perform the division. It just simply takes the integer portion of the number without regard to the numbers after the decimal point.
-2.3 as a result of integer division becomes -2
2.3 as a result of integer division becomes 2
I tested it in .NET and this is the case for that implementation of integer division at least.
Admin
Yeah, okay - but what's the WTF here? I mean, just look: descriptive variable names (and no Hungarian notation to boot!), nicely indented, just enough comments - I wish my colleagues would write code like this!
(Later) Ah, NOW I see it - he should have used public instead of private, to encourage re-use of this great piece of code!
Oh, and for those who just don't get it:
</sarcasm>
Best, Hugo
Admin
I think it's like rhetorics. Do you know what that is?
Admin
I used to think otherwise until I read your comments.
Admin
There's no rounding involved here. He is simply taking the result of a divison operation and casting it to an integer, which drops off the decimal portion. Casting -2.3 to an integer produces -2 just as casting 2.3 to an integer produces +2. Try evaluating the following (C# code):
Console.WriteLine(((<FONT style="BACKGROUND-COLOR: #ffffff" color=#0000ff>int</FONT>) -2.3).ToString());
Admin
Except integer division doesn't round, it truncates, and will therefore always go towards zero anyways.
Admin
Aye, I didn't consider that at first. However, would there be any significance to a negative return value from the function? The difference is still a positive number of milliseconds, regardless of which one went first. I suppose we'd need to see the rest of the implementation in the app for that (not that I'd especially *like* to, mind you).
But doesn't integer division just truncate the decimal anyway? Plus, based on the rest of the function, I'd be willing to bet that wasn't the coder's thought process....
Admin
Assuming there are more than ten additional bits in a long data type compared to an int data type, there's also an overflow here.
Admin
Ok, I guess my responding before reading the rest of the comments is a WTF in itself.
Admin
I know how we always try to explain why the code was written in such a way...
... I just can't come up with anything for this grand masterpiece!
Admin
Wow. Wow. This is definitely brillant.
The only mitigating factor is that the code works for all inputs.
Admin
Is that like generics in Java 5.0?
Admin
You're right in that there is a possible overflow; I didn't even notice that. Although, to hit the overflow, the difference passed in would have to be equal to just at 4082 years, so it's doubtful that it would happen in practical usage. Still, I hate to see code where a bug is possible even if it's not practical.
Admin
Do I know what rhetorical means? (8(1)
Admin
On x86 (I doubt the coder was using something else), longs and ints are both 4 bytes long.
Admin
Worse than that. 2^15 minutes is only about 23 days, so overflow hits at about 2 billion (US) milliseconds.
Wasn't there a bug in Windows like that that forced you to reboot servers after they were left on for about a month?
--RA
Admin
And for those of you just tuning in, today's WTF is brought to you by the letters J, A, V, and A.
Admin
BWAAAAHAHAHAHHAHAHAHAAHHAHA! What a tosser!
Admin
Oh, wait, integers take 32 bits these days, don't they? Gah! I'm showing my age!
--RA
Admin
Admin
With a 32-bit integer, the max value it can handle would be 2^31 - 1. This comes out to
2,147,483,647 minutes or
35,791,394.12 hours or
1,491,308.088 days or
4082.979 years (taking a year as being 365.25 days, which is not totally accurate, but close enough for this example)
Admin
To be fair, some of those zeroes are pretty exceptional.... except for the ones that aren't.